Android 2.3 Sqlite在插入时遇到麻烦

时间:2011-01-31 14:12:56

标签: android sqlite android-emulator

我有一个直截了当的问题,我想我错过了一些观点。我的测试应用程序部署在仿真器上,在AVD中,虚拟SD卡为30megs。 当我从主要活动中拨打以下内容时:

DatabaseManager.Initialize(this);
DatabaseManager.getEntities();

我的应用程序崩溃和调试器说没有这样的表!表 ITEM 在我的数据库帮助程序的onCreate()中创建, 正确调用(逐步调试)。我在那里错过了什么?

数据库管理员

public class DatabaseManager {

public static final String ITEM_CONTACT = "CONT";
public static final String ITEM_FILE = "FILE";
public static final String ITEM_APP = "AAPP";
public static final String ITEM_OPT = "OPTS";


private static final String DATABASE_NAME = "androfast.sqlite3.db";
private static final Integer DATABASE_VERSION = 1;
private static final String TABLE_ITEM = "ITEM";
private static final String TABLE_KEYWORD = "KEYWORD";

private static final String DATABASE_SCHEMA_FILE = "schema.sql";

public static final String ITEM_COLUMN_ID = "id";
public static final String ITEM_COLUMN_NAME = "name";
public static final String ITEM_COLUMN_PACKAGE = "pkg";
public static final String ITEM_COLUMN_TYPE = "kind";
public static final String ITEM_COLUMN_PRIORITY = "karma";

public static final String KEYWORD_COLUMN_ID = "id";
public static final String KEYWORD_COLUMN_VALUE = "word";
public static final String KEYWORD_COLUMN_PRIORITY = "karma";

private static SQLiteDatabase dbSqlite;
private static DatabaseHelper dbHelper;
private static Context context;

public static void Initialize(Context ctx) throws IOException{
    context = ctx;
    InputStream is = ctx.getAssets().open(DATABASE_SCHEMA_FILE);
    BufferedReader r = new BufferedReader(new InputStreamReader(is));
    StringBuilder strQuery = new StringBuilder("");

    String s;

    while ((s = r.readLine()) != null){
        strQuery.append(s);
    }
    r.close();

    dbHelper = new DatabaseHelper(ctx, DATABASE_NAME,null, DATABASE_VERSION, strQuery.toString());
    dbSqlite = dbHelper.getWritableDatabase();
}

public static SQLiteDatabase getSqliteDatabase(){
    return dbSqlite;
}

public static void runQuery(String strQuery){
    dbSqlite.execSQL(strQuery);
}

public static List<Item> getEntities(){
    ArrayList<Item> items = new ArrayList<Item>();
    Cursor cursor;

    dbSqlite.execSQL("INSERT INTO ITEM (name, pkg, kind) VALUES('Don Coreleone', 'life.thug.jail', 'CONT')");
    dbSqlite.execSQL("INSERT INTO ITEM (name, pkg, kind) VALUES('slavery.png', 'life.thug.jail', 'FILE')");
    dbSqlite.execSQL("INSERT INTO ITEM (name, pkg, kind) VALUES('Alcatraz', 'life.thug.jail', 'AAPP')");
    dbSqlite.execSQL("INSERT INTO ITEM (name, pkg, kind) VALUES('BLUETOOTH', 'life.thug.jail', 'OPTS')");
    dbSqlite.execSQL("INSERT INTO ITEM (name, pkg, kind) VALUES('Che Guevara', 'life.thug.jail', 'CONT')");

    cursor = dbSqlite.query(TABLE_ITEM, null,null,null,null,null,null);

    if (cursor.moveToFirst()){
        do{
            Log.d("APP/DB", cursor.getString(cursor.getColumnIndex(ITEM_COLUMN_NAME)));
        }while(cursor.moveToNext());
    }

    cursor.close();

    return items;
}

}

数据库助手

public class DatabaseHelper extends SQLiteOpenHelper
{
private static final String LOG_TAG = "SQLiteOpenHelper";
private String dbCreateQuery;

public DatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version, String dbCreate) {
    super(context, name, factory, version);
    dbCreateQuery = dbCreate;
}

@Override
public SQLiteDatabase getWritableDatabase() {
    return super.getWritableDatabase();    //To change body of overridden methods use File | Settings | File Templates.
}

@Override
public SQLiteDatabase getReadableDatabase() {
    return super.getReadableDatabase();    //To change body of overridden methods use File | Settings | File Templates.
}

@Override
public void close() {
    super.close();    //To change body of overridden methods use File | Settings | File Templates.
}

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
    sqLiteDatabase.execSQL(dbCreateQuery);
}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
    Log.w(LOG_TAG,"Upgrading database from version " + i + " to version " + i1);
    // No need to do drops of tables, these must be included in the schema.sql file
    onCreate(sqLiteDatabase);
}

@Override
public void onOpen(SQLiteDatabase db) {
    super.onOpen(db);
}
}

感谢你的指示fellas

亲切的问候,

Miloud B。

1 个答案:

答案 0 :(得分:0)

我遇到了这个问题!

所有内容都在以下声明中:

* public void execSQL(String sql)

自:API级别1 执行不是查询的单个 SQL语句。例如,CREATE TABLE,DELETE,INSERT等。以分号分隔的多个语句 不支持。采取写锁*