· 9 years ago · Dec 29, 2016, 12:17 PM
1public class Database {
2
3 public static final String KEY_ROWID = "ID";
4 public static final String KEY_NAME = "NAME";
5 public static final String KEY_HOTNESS = "PLACE";
6
7 private static final String DATABASE_NAME = "Database.db";
8 private static final String DATABASE_TABLE = "peopleTable";
9 private static final int DATABASE_VERSION = 6;
10
11 private static DbHelper ourHelper;
12 private final Context ourContext;
13 private static SQLiteDatabase ourDatabase;
14
15 private static class DbHelper extends SQLiteOpenHelper{
16
17 public DbHelper(Context context) {
18 super(context, DATABASE_NAME, null, DATABASE_VERSION);
19 // TODO Auto-generated constructor stub
20 }
21
22 @Override //I create it here, right?
23 public void onCreate(SQLiteDatabase db) {
24
25
26 db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
27 KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
28 KEY_NAME + " TEXT NOT NULL UNIQUE, " +
29 KEY_HOTNESS + " TEXT NOT NULL);"
30 );
31
32
33 db.execSQL("INSERT INTO " + getDatabaseTable() + "(NAME, PLACE) VALUES('10',' Plantation')");
34 db.execSQL("INSERT INTO " + getDatabaseTable() + "(NAME, PLACE) VALUES('100','NR Plantation')");
35
36
37 }
38
39 @Override
40 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
41 db.execSQL("DROP TABLE IF EXISTS " + getDatabaseTable());
42 onCreate(db);
43 }
44
45 public void close(Database database) {
46 // TODO Auto-generated method stub
47
48 }
49 }
50
51
52
53 public Database(Context c){
54 ourContext = c;
55 }
56
57
58 public Database open() throws SQLException{
59 ourHelper = new DbHelper(ourContext);
60 ourDatabase = ourHelper.getWritableDatabase();
61 return this;
62 }
63
64 public void close() {
65
66 ourHelper.close();
67}
68
69 public static String getDatabaseTable() {
70 return DATABASE_TABLE;
71 }
72
73 public List<String> getrecords() {
74 // TODO Auto-generated method stub
75 List<String> arraylist = new ArrayList<String>();
76 String[] columns = new String[]{ KEY_NAME };
77 Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
78 String result = "";
79
80
81
82 int iName = c.getColumnIndex(KEY_NAME);
83
84
85 for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
86 arraylist.add(c.getString(iName));
87
88 return arraylist;
89
90 }
91
92 return arraylist;
93 }
94
95}
96
97Your method should look like this :
98
99public ArrayList<String> getRecords(){
100 ArrayList<String> data=new ArrayList<String>();
101 Cursor cursor = db.query(DATABASE_TABLE, new String[]{"column names"},null, null, null, null, null);
102 String fieldToAdd=null;
103 while(cursor.moveToNext()){
104 fieldToAdd=cursor.getString(0);
105 data.add(fieldToAdd);
106 }
107 cursor.close(); // dont forget to close the cursor after operation done
108 return data;
109}
110
111this method will return all field values from database to arraylist
112
113ArrayList<String> listName = new ArrayList<String>();
114 Cursor cursor = db.query(YOUR_TABLE_NAME, new String[] { "name" },null, null, null, null, null);
115 if (cursor.moveToFirst()) {
116 do {
117 String name=cursor.getString(0); // Here you can get data from table and stored in ArrayList
118 listName.add(name);
119 } while (cursor.moveToNext());
120 }