1

我不确定我的问题。查询第二个表时我没有得到这样的表..这些都在 onCreate 方法中

db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_CBID
                   + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_NAME
                   + " TEXT NOT NULL, " + KEY_RACE + " TEXT NOT NULL,"
                   + KEY_CLASS + " TEXT NOT NULL," + KEY_DEITY
                   + " TEXT NOT NULL," + KEY_GENDER + " TEXT NOT NULL,"
                   + KEY_HEIGHT + " TEXT NOT NULL," + KEY_WEIGHT
                   + " TEXT NOT NULL);");

        db.execSQL("CREATE TABLE " + DATABASE_TABLE2 + " (" + KEY_CSID
                   + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_SKILL
                   + " TEXT NOT NULL, " + KEY_CBID + " INTEGER PRIMARY KEY FOREIGN KEY);"
                   );

编辑:显示 cvs

String skill = "blah test";
    Cursor c = ourDatabase.rawQuery("SELECT " + KEY_CBID + " FROM " + DATABASE_TABLE + " order by " + KEY_CBID + " DESC limit 1", null);
    if (c != null)
    {
        c.moveToFirst();
        cbid = c.getInt(0);
    }

    ContentValues cv = new ContentValues();
    cv.put(KEY_SKILL, skill);
    cv.put(KEY_CBID, cbid);
    return ourDatabase.insert(DATABASE_TABLE2, null, cv);

我的选择语句是:

Cursor c = ourDatabase.rawQuery("SELECT " + KEY_SKILL + ", " + KEY_NAME + ", " + KEY_CBID + " FROM " + DATABASE_TABLE + ", " + DATABASE_TABLE2 + " WHERE " + DATABASE_TABLE +"."+KEY_CBID+" = " +DATABASE_TABLE2+"."+KEY_CBID+" && " +DATABASE_TABLE+"."+KEY_NAME+" = '"+item+"'", null); 
4

2 回答 2

2

我不相信正在创建您的第二个表,这是使用外键声明表的方法:

CREATE TABLE artist(
  artistid    INTEGER PRIMARY KEY, 
  artistname  TEXT
);

CREATE TABLE track(
  trackid     INTEGER, 
  trackname   TEXT, 
  trackartist INTEGER,
  FOREIGN KEY(trackartist) REFERENCES artist(artistid)
);

我从SQLite.org拿了这些。

SQLite 中默认禁用外键,供 Android 使用:

db.execSQL("PRAGMA FOREIGN_KEYS=ON;");

在插入数据之前,如果你还没有;

于 2012-05-05T19:23:43.017 回答
0
  1. 检查您的日志 cat 以查看在创建第二个表时是否有错误。如果 sqllite 出现错误,会在 logcat 中记录

  2. 将新表添加到数据库时,如果您正在调用 db.open ,请确保从您的代码升级数据库,如果数据库已经创建,它将打开。所以在这种情况下,创建新表的代码不会被命中。因此,您必须确保升级数据库,您可以在其中删除所有现有表并重新创建

    @Override public void onCreate(SQLiteDatabase db) {

        createAllTables(db);
        Log.v(DBTAG, "DATABASE CREATED");
        // Toast.makeText(mCtx, "created", Toast.LENGTH_SHORT).show();
    }
    
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(DBTAG, "Upgrading database from version " + oldVersion
                + " to " + newVersion + ", which will destroy all old data");
        dropExistingTables(db);
        onCreate(db);
    }
    
    private void dropExistingTables(SQLiteDatabase db) {
    
        for (int i = 0; i < Utils.getDropTableQueries().size(); i++) {
            db.execSQL(Utils.getDropTableQueries().get(i));
        }
    }
    

因此得出结论,请确保您正在创建表,并且在创建时没有错误。

于 2012-05-06T07:11:24.660 回答