如何在sqlite中从动态创建的数据库中检索值

时间:2013-12-27 11:59:39

标签: android database sqlite

我创建了没有静态名称的数据库。数据库已创建,表也在该数据库中创建,值也插入到该数据库中,但是当我通过游标从数据库中检索值时,值不会从数据库返回。

数据库助手类的代码

  public class DatabaseClass extends SQLiteOpenHelper {
    public static final int DATABASE_VERSION = 3;
/*  public static final String DATABASE_NAME = "FeetFork_Final_DataBase";*/
    public static final String TABLE_CAMPAIGN = "Campaign_Details";


    public SQLiteDatabase db;
   public DatabaseClass(Context context) {
        super(context, database_name, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        System.out.println("oncreateeeee");
        db.execSQL(Create_tables_campaign("Campaign"));
    }

    public String Create_tables_campaign(String a) {
        String CREATE_CAMPAIGN_TABLE = "CREATE TABLE IF NOT EXISTS "
                + DatabaseClass.TABLE_CAMPAIGN + "(" + a
                + " TEXT PRIMARY KEY " + ")";
        return CREATE_CAMPAIGN_TABLE;
    }


    // Upgrading database
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_CAMPAIGN);
        onCreate(db);
    }

获取记录的代码......

Cursor c1 = db.query(DatabaseClass.TABLE_CAMPAIGN,columns_campaigns, null, null, null, null, null);
            c1.moveToFirst();
            while (!c1.isAfterLast()) {
                alist_campaign.add(c1.getString(0));
                c1.moveToNext();
            }

3 个答案:

答案 0 :(得分:0)

尝试检查数据库版本,如果相同则不会更改数据库的更改。

答案 1 :(得分:0)

试试这个:

Cursor c1 = db.query(DatabaseClass.TABLE_CAMPAIGN,columns_campaigns, null, null, null, null, null);
while (c1.moveToNext) {
   alist_campaign.add(c1.getString(0));
}
c1.close();

修改

通过记录:

检查Cursor是否获取值
Log.i("cursor length", c1.getCount()+"");

答案 2 :(得分:0)

试试这个,

Cursor c1 = db.query(DatabaseClass.TABLE_CAMPAIGN,columns_campaigns, null, null, null, null, null);
                c1.moveToFirst();
                while (!c1.isAfterLast()) 
               {
                    alist_campaign.add(c1.getString(c1.getColumnIndex("Your_Column_Name")));
                    c1.moveToNext();
                }