· 7 years ago · Jan 29, 2019, 12:58 AM
1package crudsqlite;
2
3import android.content.ContentValues;
4import android.content.Context;
5import android.database.Cursor;
6import android.database.sqlite.SQLiteDatabase;
7import android.database.sqlite.SQLiteOpenHelper;
8import android.util.Log;
9
10import java.util.ArrayList;
11import java.util.HashMap;
12
13public class DbHelper extends SQLiteOpenHelper {
14
15 private static final int DATABASE_VERSION = 2;
16
17 static final String DATABASE_NAME = "atlasgalon";
18
19 public static final String TABLE_SQLite = "sqlite";
20
21 public static final String COLUMN_ID = "id";
22 public static final String COLUMN_NAME = "name";
23 public static final String COLUMN_ADDRESS = "address";
24
25 public DbHelper(Context context) {
26 super(context, DATABASE_NAME, null, DATABASE_VERSION);
27 }
28
29 @Override
30 public void onCreate(SQLiteDatabase db) {
31 final String SQL_CREATE_MOVIE_TABLE = "CREATE TABLE " + TABLE_SQLite + " (" +
32 COLUMN_ID + " INTEGER PRIMARY KEY autoincrement, " +
33 COLUMN_NAME + " TEXT NOT NULL, " +
34 COLUMN_ADDRESS + " TEXT NOT NULL" +
35 " )";
36
37 db.execSQL(SQL_CREATE_MOVIE_TABLE);
38 }
39
40 @Override
41 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
42 db.execSQL("DROP TABLE IF EXISTS " + TABLE_SQLite);
43 onCreate(db);
44 }
45
46 public ArrayList<HashMap<String, String>> getAllData() {
47 ArrayList<HashMap<String, String>> wordList;
48 wordList = new ArrayList<HashMap<String, String>>();
49 String selectQuery = "SELECT * FROM " + TABLE_SQLite;
50 SQLiteDatabase database = this.getWritableDatabase();
51 Cursor cursor = database.rawQuery(selectQuery, null);
52 if (cursor.moveToFirst()) {
53 do {
54 HashMap<String, String> map = new HashMap<String, String>();
55 map.put(COLUMN_ID, cursor.getString(0));
56 map.put(COLUMN_NAME, cursor.getString(1));
57 map.put(COLUMN_ADDRESS, cursor.getString(2));
58 wordList.add(map);
59 } while (cursor.moveToNext());
60 }
61
62 Log.e("select sqlite ", "" + wordList);
63
64 database.close();
65 return wordList;
66 }
67
68 public void insert(String name, String address) {
69 SQLiteDatabase database = this.getWritableDatabase();
70 String queryValues = "INSERT INTO " + TABLE_SQLite + " (name, address) " +
71 "VALUES ('" + name + "', '" + address + "')";
72
73 Log.e("insert sqlite ", "" + queryValues);
74 database.execSQL(queryValues);
75 database.close();
76 }
77
78 public void update(int id, String name, String address) {
79 SQLiteDatabase database = this.getWritableDatabase();
80
81 String updateQuery = "UPDATE " + TABLE_SQLite + " SET "
82 + COLUMN_NAME + "='" + name + "', "
83 + COLUMN_ADDRESS + "='" + address + "'"
84 + " WHERE " + COLUMN_ID + "=" + "'" + id + "'";
85 Log.e("update sqlite ", updateQuery);
86 database.execSQL(updateQuery);
87 database.close();
88 }
89
90 public void delete(int id) {
91 SQLiteDatabase database = this.getWritableDatabase();
92
93 String updateQuery = "DELETE FROM " + TABLE_SQLite + " WHERE " + COLUMN_ID + "=" + "'" + id + "'";
94 Log.e("update sqlite ", updateQuery);
95 database.execSQL(updateQuery);
96 database.close();
97 }
98
99}