· 7 years ago · Jan 13, 2019, 07:04 PM
1cant added items to db on android
2DBAdapter dbAdper=new DBAdapter(this);
3 dbAdper.open();
4 Cursor CursorDb =dbAdper.getAllTitles();
5
6 //dbAdper.dropTable();
7 dbAdper.insertTitle("rsstitle", "here need to be link");
8 dbAdper.insertTitle("rsstitle2", "here need to be link2");
9 dbAdper.insertTitle("rsstitle3", "here need to be link3");
10
11 int n=CursorDb.getCount();
12 do
13 {
14 String s= CursorDb.getString(CursorDb.getColumnIndex("RssTitle"));
15 list.add(s);
16
17 }while(CursorDb.moveToNext());
18 CursorDb.close();
19
20public class DBAdapter {
21
22 private static final int DATABASE_VERSION = 1;
23
24 private static final String DATABASE_CREATE =
25 "create table titles (_id integer primary key autoincrement, "
26 + "link text not null, RssTitle text not null);";
27
28 private final Context context;
29
30 private DatabaseHelper DBHelper;
31 private SQLiteDatabase db;
32
33 public DBAdapter(Context ctx)
34 {
35 this.context = ctx;
36 DBHelper = new DatabaseHelper(context);
37 }
38
39 private static class DatabaseHelper extends SQLiteOpenHelper
40 {
41 DatabaseHelper(Context context)
42 {
43 super(context, DATABASE_NAME, null, DATABASE_VERSION);
44 }
45
46 @Override
47 public void onCreate(SQLiteDatabase db)
48 {
49 db.execSQL(DATABASE_CREATE);
50 }
51
52public void dropTable() throws SQLException
53 {
54 //DROP TABLE IF EXISTS mydatabase.myTable
55 db.execSQL("DROP TABLE IF EXISTS "+DATABASE_NAME+"."+DATABASE_TABLE);
56 }
57//---insert a title into the database---
58public long insertTitle(String insertLink, String title)
59{
60 ContentValues initialValues = new ContentValues();
61 initialValues.put(link, insertLink);
62 initialValues.put(RssTitle, title);
63
64 return db.insert(DATABASE_TABLE, null, initialValues);
65}
66
67//---retrieves all the titles---
68public Cursor getAllTitles()
69{
70 return db.query(DATABASE_TABLE, new String[] {
71 KEY_ROWID,
72 link,
73 RssTitle,
74 },
75 null,
76 null,
77 null,
78 null,
79 null
80 );
81}