我有一个包含很多字段的活动,当我点击一个按钮时,所有值都存储在一个sqlite数据库中。
当我的所有字段都完成后,这项工作正常,但我也希望它在字段为空时工作,因为我的目标是保存字段的状态以便以后完成它们。
但是,当我尝试这样做时,我遇到了这个问题:
E/SQLiteDatabase( 1542): Error inserting
E/SQLiteDatabase( 1542): android.database.sqlite.SQLiteException: near "null": syntax error: , while compiling: INSERT INTO IN_MISSION(null) VALUES (NULL)
E/SQLiteDatabase( 1542): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
E/SQLiteDatabase( 1542): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:68)
E/SQLiteDatabase( 1542): at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:143)
E/SQLiteDatabase( 1542): at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:361)
E/SQLiteDatabase( 1542): at android.database.sqlite.SQLiteStatement.acquireAndLock(SQLiteStatement.java:260)
E/SQLiteDatabase( 1542): at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:112)
E/SQLiteDatabase( 1542): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1718)
E/SQLiteDatabase( 1542): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1591)
E/SQLiteDatabase( 1542): at com.soft2rent.unicarmobile.DbManagement.insert(DbManagement.java:49)
E/SQLiteDatabase( 1542): at com.soft2rent.unicarmobile.SaisieMission.onOptionsItemSelected(SaisieMission.java:75)
E/SQLiteDatabase( 1542): at android.app.Activity.onMenuItemSelected(Activity.java:2502)
E/SQLiteDatabase( 1542): at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:373)
E/SQLiteDatabase( 1542): at android.support.v7.app.ActionBarActivity.superOnMenuItemSelected(ActionBarActivity.java:245)
E/SQLiteDatabase( 1542): at android.support.v7.app.ActionBarActivityDelegateICS.onMenuItemSelected(ActionBarActivityDelegateICS.java:164)
E/SQLiteDatabase( 1542): at android.support.v7.app.ActionBarActivity.onMenuItemSelected(ActionBarActivity.java:131)
E/SQLiteDatabase( 1542): at android.support.v7.app.ActionBarActivityDelegateICS$WindowCallbackWrapper.onMenuItemSelected(ActionBarActivityDelegateICS.java:308)
E/SQLiteDatabase( 1542): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:950)
E/SQLiteDatabase( 1542): at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
E/SQLiteDatabase( 1542): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
E/SQLiteDatabase( 1542): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
E/SQLiteDatabase( 1542): at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:490)
E/SQLiteDatabase( 1542): at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:108)
E/SQLiteDatabase( 1542): at android.view.View.performClick(View.java:3511)
E/SQLiteDatabase( 1542): at android.view.View$PerformClick.run(View.java:14105)
E/SQLiteDatabase( 1542): at android.os.Handler.handleCallback(Handler.java:605)
E/SQLiteDatabase( 1542): at android.os.Handler.dispatchMessage(Handler.java:92)
E/SQLiteDatabase( 1542): at android.os.Looper.loop(Looper.java:137)
E/SQLiteDatabase( 1542): at android.app.ActivityThread.main(ActivityThread.java:4424)
E/SQLiteDatabase( 1542): at java.lang.reflect.Method.invokeNative(Native Method)
E/SQLiteDatabase( 1542): at java.lang.reflect.Method.invoke(Method.java:511)
E/SQLiteDatabase( 1542): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
E/SQLiteDatabase( 1542): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
E/SQLiteDatabase( 1542): at dalvik.system.NativeStart.main(Native Method)
以下是我的查询代码:
public long insert(Mission mission) {
ContentValues cv = new ContentValues();
cv = mission.getAll();
return db.insert("IN_MISSION", null, cv);
}
我想要的是能够在应用程序中为空时将字段设置为空! 谢谢你的阅读!
答案 0 :(得分:0)
您可以使用以下内容插入数据....
ContentValues contentvalue = new ContentValues();
contentvalue.put(TaskDatabaseHelper.FRND_USERNAME,uname); // column name : data..
contentvalue.put(TaskDatabaseHelper.FRND_FNAME, fname);
contentvalue.put(TaskDatabaseHelper.FRND_LNAME, lname);
contentvalue.put(TaskDatabaseHelper.FRND_EMAIL, email);
sqlitedb.insert(TaskDatabaseHelper.FRIENDS_TABLE_NAME, null, contentvalue); // adding data...
如果您有空白数据,则插入""
(空白数据)但请确保您的列接受空白数据并且未声明为“NOT NULL”......