添加和修改时处理本地数据库时出现问题

时间:2019-07-11 06:53:06

标签: android sqlite android-sqlite

编辑和添加数据库时遇到问题

我试图编辑和搜索问题,但是找不到解决方法

   public boolean insert_dealer(get_set_db dealer){

        ContentValues Values = new ContentValues();
       Values.put(My_data.dealer_CLN_NMEA  , dealer.getName());
       Values.put(My_data.dealer_CLN_description , dealer.getDescribe());
       Values.put(My_data.dealer_CLN_image , dealer.getImage());
       Values.put(My_data.dealer_CLN_WHOLESALE , dealer.getWholesale());
       Values.put(My_data.dealer_CLN_INDIVIDUAL , dealer.getAn_individual());

       long result = database.insert(My_data.dealer_TB_NAME,null,Values);
       return result != -1;
   }

日志详细信息


2019-07-11 09:28:44.341 31418-31418/com.example.dealer E/SQLiteLog: (1) near "individual": syntax error
2019-07-11 09:28:44.348 31418-31418/com.example.dealer E/SQLiteDatabase: Error inserting image=kcc An individual=3.0 describe=othman wholesale=2.0 Name=ott
    android.database.sqlite.SQLiteException: near "individual": syntax error (code 1 SQLITE_ERROR): , while compiling: INSERT INTO dealerr(image,An individual,describe,wholesale,Name) VALUES (?,?,?,?,?)
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:903)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:514)
        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.dealer.DatabaseAccess.insert_dealer(DatabaseAccess.java:49)
        at com.example.dealer.Main2Activity.onOptionsItemSelected(Main2Activity.java:154)

2 个答案:

答案 0 :(得分:0)

在这里Dealer.getAn_CLN_INDIVIDUAL()返回“个人”,此处您不能使用空格保留属性名称。只需将其更改为an_individual,希望它可以工作。

My_data.dealer_CLN_INDIVIDUAL , dealer.getAn_individual()

答案 1 :(得分:0)

代表列名的常量My_data.dealer_CLN_INDIVIDUAL已定义为:

String dealer_CLN_INDIVIDUAL = "An individual";

列名不能包含空格。
您可以将其括在方括号中以解决问题:

String dealer_CLN_INDIVIDUAL = "[An individual]";