在插入项目详细信息时出现这样的错误:
09-28 16:30:32.558 9471-9471/com.example.android.inventoryapp E/SQLiteDatabase: Error inserting quantity=1 name=Chocolate phone num=9112 price=5 supp name=Venu
android.database.sqlite.SQLiteException: near "num": syntax error (code 1): , while compiling: INSERT INTO inventory(quantity,name,phone num,price,supp name) VALUES (?,?,?,?,?)
数据库助手为:
public void onCreate(SQLiteDatabase sqLiteDatabase) {
String SQL_CREATE_PRODUCTS_TABLE = "CREATE TABLE " + InventoryContract.InventoryEntry.TABLE_NAME + " ("
+ InventoryContract.InventoryEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ InventoryContract.InventoryEntry.COLUMN_Product_Name + " TEXT NOT NULL, "
+ InventoryContract.InventoryEntry.COLUMN_PRICE + " INTEGER NOT NULL, "
+ InventoryContract.InventoryEntry.COLUMN_Quantity + " INTEGER NOT NULL DEFAULT 0, "
+ InventoryContract.InventoryEntry.COLUMN_SUPPLIER_NAME + " TEXT NOT NULL, "
+ InventoryContract.InventoryEntry.COLUMN_SUPPLIER_Phno + " INTEGER NOT NULL);";
// Execute the SQL statement
//COLUMN_USERNAME + " TEXT "
sqLiteDatabase.execSQL(SQL_CREATE_PRODUCTS_TABLE);
}
合同类为:
public final static class InventoryEntry implements BaseColumns{
public final static String TABLE_NAME="inventory";
public final static String _ID = BaseColumns._ID;
public final static String COLUMN_Product_Name="name";
public final static String COLUMN_PRICE="price";
public final static String COLUMN_Quantity="quantity";
public final static String COLUMN_SUPPLIER_NAME="supp name";
public final static String COLUMN_SUPPLIER_Phno="phone num";
}
logcat的响应是:
com.example.android.inventoryapp E/SQLiteLog: (1) near "num": syntax error
09-28 16:30:32.558 9471-9471/com.example.android.inventoryapp E/SQLiteDatabase: Error inserting quantity=1 name=Chocolate phone num=9112 price=5 supp name=Venu
android.database.sqlite.SQLiteException: near "num": syntax error (code 1): , while compiling: INSERT INTO inventory(quantity,name,phone num,price,supp name) VALUES (?,?,?,?,?)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
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:1469)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341)
at com.example.android.inventoryapp.CatalogActivity.insertpet(CatalogActivity.java:104)
at com.example.android.inventoryapp.CatalogActivity.onOptionsItemSelected(CatalogActivity.java:118)
at android.app.Activity.onMenuItemSelected(Activity.java:2885)
at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:407)
at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:195)`
答案 0 :(得分:3)
列名中不能有空格。如果要像使用phone num
一样使用空格,则应使用双引号("
)来对名称进行转义:
INSERT INTO inventory("quantity", "name", "phone num", "price", "supp name")
VALUES (?, ?, ?, ?, ?)
答案 1 :(得分:1)
Create a method and write the code to insert the data into table as below...
public boolean insertPetition1(String cap_date, String cap_time, String image_str, String latitude, String longitude,
String upload_status) {
SQLiteDatabase db = getWritableDatabase();
try {
ContentValues contentValues = new ContentValues();
contentValues.put(COLUMN_IMAGE_PATH, image_str);
contentValues.put(COLUMN_CAPTURE_DATE, cap_date);
contentValues.put(COLUMN_CAPTURE_TIME, cap_time);
contentValues.put(COLUMN_LATITUDE, latitude);
contentValues.put(COLUMN_LONGITUDE, longitude);
contentValues.put(COLUMN_UPLOAD_STATUS, upload_status);
if (db.insert(MAIN_TABLE, null, contentValues) > 0) {
Log.e("Parameters", "Parameters:" + contentValues);
isInserted = true;
}
} catch (Exception e) {
Log.v("TAG", e.toString());
}
return isInserted;
}
Call this method where ever you want to insert data in the tabel ....
public void dbChanges() {
DatabaseHelper1 databaseMain = new
DatabaseHelper1(OfflineAttendanceChildWithOutAuto.this);
boolean status = false;
status = databaseMain.insertPetition1(date, currentTime, encodedImage, latitude, longitude, "", "", "", awccode, "", "", mainaddress, usercode, "", "", "1");
databaseMain.close();
}
It will work for sure try like this....