无法将数据插入我的第二个表,sqlite android

时间:2014-07-31 11:27:00

标签: android sqlite

``我的sqLite数据库中有两个表,第一个表工作正常,但我无法将数据插入第二个表。我有一个dbadapter类,我在onCreate()方法中使用contentValues在两个表中插入数据。

我尝试了几乎所有的东西来使它工作,但一切似乎都没用 这是我的dbadapter代码`

 public class MainData {
    private static final String DATABASE_NAME = "Homeomedicaldb";
private static final int DATABASE_VERSION = 3;

private static final String DATABASE_TABLE1 = "basic_table";
private static final String KEY_ID = "sym_id";
private static final String KEY_NAME = "sym_detail_name";

private static final String DATABASE_TABLE2 = "detail_table";
private static final String KEY_SYM_ID = "sym_previous_id";
private static final String KEY_DET_ID = "sym_detail_id";
private static final String KEY_SYMP_DETAIL = "sym_details";
private static final String KEY_MEDICINE = "medicine_name";

private static final String CREATE_DATABASE_TABLE1 = "CREATE TABLE IF NOT EXISTS " + DATABASE_TABLE1 + " (" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_NAME + " TEXT NOT NULL);";
private static final String CREATE_DATABASE_TABLE2 = "CREATE TABLE IF NOT EXISTS " + DATABASE_TABLE2 + " (" + KEY_DET_ID + " INTEGER PRIMARY KEY, " + KEY_SYMP_DETAIL + " text not null, " + KEY_MEDICINE + " text not null, " + KEY_SYM_ID + " integer, "+ " FOREIGN KEY ("+KEY_SYM_ID+") REFERENCES "+DATABASE_TABLE1+" ("+KEY_ID+"));" ;


private DbHelper ourHelper;
private final Context ourContext;
private static SQLiteDatabase ourDatabase;


private static class DbHelper extends SQLiteOpenHelper {

    public DbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
            db.execSQL(CREATE_DATABASE_TABLE1);
            db.execSQL(CREATE_DATABASE_TABLE2); 
            db.execSQL("PRAGMA foreign_keys = ON;");


            ContentValues values = new ContentValues();
            values.put(KEY_ID, "1");
            values.put(KEY_NAME, "headache");
            db.insert(DATABASE_TABLE1, null, values);

            values.put(KEY_ID, "2");
            values.put(KEY_NAME, "abdominal pain");
            db.insert(DATABASE_TABLE1, null, values);

            values.put(KEY_ID, "3");
            values.put(KEY_NAME, "eye infection");
            db.insert(DATABASE_TABLE1, null, values);

            values.put(KEY_ID, "4");
            values.put(KEY_NAME, "skin pimples");
            db.insert(DATABASE_TABLE1, null, values);

            values.put(KEY_ID, "5");
            values.put(KEY_NAME, "infection");
            db.insert(DATABASE_TABLE1, null, values);
 ContentValues new_values = new ContentValues();    

new_values.put(KEY_DET_ID, "1.1");
            new_values.put(KEY_SYMP_DETAIL, "eye;inflammation;with headache");
            new_values.put(KEY_MEDICINE, "lycopodium");
            new_values.put(KEY_SYM_ID, "1");
            return ourDatabase.insert(DATABASE_TABLE2, null, new_values);   
      new_values.put(KEY_DET_ID, "1.2");
            new_values.put(KEY_SYMP_DETAIL, "nose;pain;with headache");
            new_values.put(KEY_MEDICINE, "lycopodium");
            new_values.put(KEY_SYM_ID, "1");
            db.insert(DATABASE_TABLE2, null, new_values);

            new_values.put(KEY_DET_ID, "1.3");
            new_values.put(KEY_SYMP_DETAIL, "perspiration;cold;with headache");
            new_values.put(KEY_MEDICINE, "lycopodium");
            new_values.put(KEY_SYM_ID, "1");
            db.insert(DATABASE_TABLE2, null, new_values);

            new_values.put(KEY_DET_ID, "1.4");
            new_values.put(KEY_SYMP_DETAIL, "face;heat;with headache");
            new_values.put(KEY_MEDICINE, "lycopodium");
            new_values.put(KEY_SYM_ID, "1");
            db.insert(DATABASE_TABLE2, null, new_values);

            new_values.put(KEY_DET_ID, "1.5");
            new_values.put(KEY_SYMP_DETAIL, "perspiration;with headache");
            new_values.put(KEY_MEDICINE, "lycopodium");
            new_values.put(KEY_SYM_ID, "1");
            db.insert(DATABASE_TABLE2, null, new_values); 


    }

这是我用来显示第二张表的结果的方法

    public String getDetail() {
    // TODO Auto-generated method stub
    String[] columns2 = new String[]{ KEY_DET_ID, KEY_SYMP_DETAIL, KEY_MEDICINE, KEY_SYM_ID };
    Cursor v = ourDatabase.query(DATABASE_TABLE2, columns2, null, null, null, null, null);
    String result_new= "";

    int iSym= v.getColumnIndex(KEY_SYM_ID);
    int iName = v.getColumnIndex(KEY_DET_ID);
    int iDetail = v.getColumnIndex(KEY_SYMP_DETAIL);
    int iMedicine = v.getColumnIndex(KEY_MEDICINE);
    for(v.moveToFirst(); !v.isAfterLast(); v.moveToNext()) {
        result_new= result_new+ v.getString(iSym)+ " " + v.getString(iName)+ " " + v.getString(iDetail)+ " " + v.getString(iMedicine) + "\n" ;
    }
    return result_new;
}

这里是logcat

    07-31 06:52:38.731: W/IInputConnectionWrapper(1263): showStatusIcon on inactive InputConnection
07-31 06:52:44.221: I/Choreographer(1263): Skipped 90 frames!  The application may be doing too much work on its main thread.
07-31 06:55:18.651: D/gralloc_goldfish(1319): Emulator without GPU emulation detected.
07-31 06:55:19.481: I/Choreographer(1319): Skipped 86 frames!  The application may be doing too much work on its main thread.
07-31 06:55:22.271: I/Choreographer(1319): Skipped 44 frames!  The application may be doing too much work on its main thread.
07-31 06:55:39.381: I/Choreographer(1319): Skipped 65 frames!  The application may be doing too much work on its main thread.
07-31 06:56:22.861: I/Choreographer(1319): Skipped 32 frames!  The application may be doing too much work on its main thread.
07-31 06:56:32.331: I/Choreographer(1319): Skipped 39 frames!  The application may be doing too much work on its main thread.
07-31 08:00:56.271: I/Choreographer(1319): Skipped 98 frames!  The application may be doing too much work on its main thread.
07-31 08:01:00.841: I/Choreographer(1319): Skipped 72 frames!  The application may be doing too much work on its main thread.
07-31 08:01:28.291: I/Choreographer(1319): Skipped 75 frames!  The application may be doing too much work on its main thread.
07-31 08:01:45.241: I/Choreographer(1319): Skipped 96 frames!  The application may be doing too much work on its main thread.
07-31 08:20:01.821: I/Choreographer(1319): Skipped 31 frames!  The application may be doing too much work on its main thread.

2 个答案:

答案 0 :(得分:2)

尝试删除第64行上的return并将ourDatabase更改为db

return ourDatabase.insert(DATABASE_TABLE2, null, new_values);   

db.insert(DATABASE_TABLE2, null, new_values);   

答案 1 :(得分:0)

<强>解决方案:

我找到了解决这个问题的方法,发布这个可能对其他人有帮助, 我只是更改了我的数据库的名称,代码工作得很好。这是因为SQLiteOpenHelper似乎在注册数据库时会遇到一些麻烦。它找到一个具有相同名称和版本号的数据库,并且可以使用#me;&#39;并没有看到结构是否完全不同。