Android上的SQLite DB列顺序问题

时间:2018-07-31 10:22:22

标签: java android sql database sqlite

我的应用程序可以正常工作(从输入中的某些用户数据生成代码和相对条形码),但是我想将数据存储在带有sqlite的Db中。这是我的DatabaseOpenHelper类:

public class DatabaseOpenHelper extends SQLiteOpenHelper {
    public static final String DATABASE_NAME = "cf_db.db";
    public static final String TABLE_NAME = "cf_table";
    public static final String CF = "CF";
    public static final String COL1 = "Name";
    public static final String COL2 = "Surname";
    public static final String COL3 = "Sex";
    public static final String COL4 = "Birthday";
    public static final String COL5 = "PlaceOfBirth";


    public DatabaseOpenHelper(Context context) {
        super(context, DATABASE_NAME, null, 1);
        //SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL("CREATE TABLE " + TABLE_NAME +
                "(" + CF + "TEXT PRIMARY KEY, " + COL1 + "TEXT NOT NULL, " +
                COL2 + "TEXT NOT NULL," + COL3 + "TEXT NOT NULL," + COL4 +
                "TEXT NOT NULL," + COL5 + "TEXT NOT NULL);");

    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(sqLiteDatabase);
    }

    public boolean insertData(String cf, String name, String surname, String sex, String year,
                              String month, String day, String place) {
        String date = day + "/" + month + "/" + year;
        SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(CF, cf);
        contentValues.put(COL1, name);
        contentValues.put(COL2, surname);
        contentValues.put(COL3, sex);
        contentValues.put(COL4, date);
        contentValues.put(COL5, place);
        sqLiteDatabase.insert(TABLE_NAME, null, contentValues);
        sqLiteDatabase.close();

    }
}

最后插入语句有问题。我遇到以下错误:

E/SQLiteLog: (1) table cf_table has no column named Birthday
E/SQLiteDatabase: Error inserting Birthday=22/08/21 CF=GGUTUU21M22I754G Surname= ggu Name=uut Sex=M PlaceOfBirth=Siracusa
                  android.database.sqlite.SQLiteException: table cf_table has no column named Birthday (code 1 SQLITE_ERROR): , while compiling: INSERT INTO cf_table(Birthday,CF,Surname,Name,Sex,PlaceOfBirth) VALUES (?,?,?,?,?,?)
                      at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                      at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:901)
                      at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:512)
                      at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
                      at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
                      at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
                      at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1562)
                      at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1433)
                      at com.example.valerio.androidcodesgenerator.DatabaseOpenHelper.insertData(DatabaseOpenHelper.java:53)
                      at com.example.valerio.androidcodesgenerator.MainActivity.AddData(MainActivity.java:136)
                      at com.example.valerio.androidcodesgenerator.MainActivity$1.onClick(MainActivity.java:98)
                      at android.view.View.performClick(View.java:6597)
                      at android.view.View.performClickInternal(View.java:6574)
                      at android.view.View.access$3100(View.java:778)
                      at android.view.View$PerformClick.run(View.java:25883)
                      at android.os.Handler.handleCallback(Handler.java:873)
                      at android.os.Handler.dispatchMessage(Handler.java:99)
                      at android.os.Looper.loop(Looper.java:193)
                      at android.app.ActivityThread.main(ActivityThread.java:6642)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

列是我不理解的随机顺序,当我使用一些打印语句分析问题时,我意识到各种contentValues根本没有任何价值。我只是做

Log.d("code", contentValues.getAsString(cf))

该错误就像“ println需要打印某些内容”,因此contentValues的put语句基本上不会放入任何内容。实际上,错误消息中的值是(??????)。 。 在通话中,打印测试进行得很好,并且各种editText和textView具有适当的内容。

这是insertData调用:

public void AddData() {
    boolean inserted = myDb.insertData(textView_cf.getText().toString(),
        editText_name.getText().toString(),
        editText_surname.getText().toString(),
        editText_sex.getText().toString(),
        editText_aa.getText().toString(),
        editText_mm.getText().toString(),
        editText_gg.getText().toString(),
        autoCompleteTextView_place.getText().toString());
}

(我还需要对插入进行布尔控制,但现在暂时不进行插入)

也许是新手错误,但我只是Android Studio的新手,根本不是Java专家...

2 个答案:

答案 0 :(得分:0)

您在CREATE TABLE语句中缺少空格

sqLiteDatabase.execSQL("CREATE TABLE " + TABLE_NAME + "(" + 
    CF   + " TEXT PRIMARY KEY, " + 
    COL1 + " TEXT NOT NULL, " +
    COL2 + " TEXT NOT NULL, " + 
    COL3 + " TEXT NOT NULL, " +
    COL4 + " TEXT NOT NULL, " + 
    COL5 + " TEXT NOT NULL)");

答案 1 :(得分:0)

您必须做的一件事是从要对其进行测试的仿真器/设备中卸载该应用程序,然后再次运行它以重新创建数据库。如果仍然显示有关Birthday字段的错误,则问题出在其他地方。

使用此插入方法:

public boolean insertData(String cf, String name, String surname, String sex, String year,
                          String month, String day, String place) {
    String date = day + "/" + month + "/" + year;
    SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(CF, cf);
    contentValues.put(COL1, name);
    contentValues.put(COL2, surname);
    contentValues.put(COL3, sex);
    contentValues.put(COL4, date);
    contentValues.put(COL5, place);
    int id = sqLiteDatabase.insert(TABLE_NAME, null, contentValues);
    sqLiteDatabase.close();

    return (id != -1);

}

我不确定这是否可以解决问题,但是通过方法的签名,它必须返回布尔值。