我创建了一个处理程序类,它使用几个方法作为插入/删除,并从表中返回一个特定的值。但是,当我在活动中创建处理程序并尝试插入产品类(将其视为字符串)时,在此之前,应用程序崩溃。我检查product
变量返回一个正确的字符串,一切似乎都很好,我一直得到下面的错误。非常感谢!
cerygo.grocerygo E/AndroidRuntime: FATAL EXCEPTION: main
Process: ca.comp3004.grocerygo.grocerygo, PID: 10703
android.database.sqlite.SQLiteException: near "productname": syntax error (code 1): , while compiling: CREATE TABLE products(_id INTEGER PRIMARY KEY AUTOINCREMENT productname TEXT );
这是我的处理程序类
public class MyDBHandler extends SQLiteOpenHelper{
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "productList.db";
public static final String TABLE_PRODUCTS = "products";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_PRODUCTNAME = "productname";
public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}
public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version, DatabaseErrorHandler errorHandler) {
super(context, name, factory, version, errorHandler);
}
@Override
public void onCreate(SQLiteDatabase db) {
String query = "CREATE TABLE " + TABLE_PRODUCTS + "(" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT " + COLUMN_PRODUCTNAME + " TEXT " + ");";
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_PRODUCTS);
onCreate(db);
}
//Add new a row
public void addProduct(Product product){
ContentValues values = new ContentValues();
values.put(COLUMN_PRODUCTNAME, product.get_productName());
SQLiteDatabase db = getWritableDatabase();
db.insert(TABLE_PRODUCTS, null, values);
db.close();
}
//Delete product
public void deleteProduct(String productName){
SQLiteDatabase db = getWritableDatabase();
db.execSQL("DELETE FROM " + TABLE_PRODUCTS + " WHERE "+ COLUMN_PRODUCTNAME + "=\"" + productName + "\";");
}
public ArrayList<String> dbToAS(){
ArrayList<String> dbAS = null;
SQLiteDatabase db = getWritableDatabase();
String query = "SELECT * FROM " + TABLE_PRODUCTS + " WHERE 1";
//Cursor pointer
Cursor c = db.rawQuery(query, null);
//move to first row
c.moveToFirst();
while(!c.isAfterLast()){
if(c.getString(c.getColumnIndex("productname")) != null){
dbAS.add(c.getString(c.getColumnIndex("productname")));
}
}
db.close();
return dbAS;
}
}
使用处理程序的活动
MyDBHandler dbHandler = new MyDBHandler(this, null, null, 1);
product = data.getString("Product");
productName.setText("Product: " + product);
//Individual request
Button button = (Button) findViewById(R.id.addItem);
final String finalProduct = data.getString("Product");
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
dbHandler.addProduct(new Product(product));
Toast.makeText(ItemPop.this, "Added", Toast.LENGTH_SHORT).show();
}
});
答案 0 :(得分:2)
您需要用逗号分隔列。
您正在尝试执行以下语句:
CREATE TABLE products(_id INTEGER PRIMARY KEY AUTOINCREMENT productname TEXT);
你应该执行这个:
CREATE TABLE products(_id INTEGER PRIMARY KEY AUTOINCREMENT, productname TEXT);