创建表上的Android SqLite异常

时间:2016-12-26 07:35:09

标签: java android mysql sqlite mobile

编辑 - 我解决了我所面临的问题,因为逗号是查询中的问题,但现在我没有得到所需的输出,即当我向数据库添加新信息时它没有得到在文本视图中显示(打印)。你们可以检查printDatabase()函数中是否有错误(虽然我没有在log-cat监视器中收到错误)。

MainActivity.java

Products.java文件

 package com.instinctcode.sqlitesample;



public class MainActivity extends AppCompatActivity {
EditText buckysInput;
TextView buckysText;
MyDBHandler dbHandler;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    buckysInput=(EditText)findViewById(R.id.buckysInput);
    buckysText=(TextView)findViewById(R.id.buckysText);
    dbHandler= new MyDBHandler(this,null,null,1);
    printDatabase();

}
public void addButtonClicked(View view){
    Products product=new Products(buckysInput.getText().toString());
    dbHandler.addProduct(product);
    printDatabase();
}
public void deleteButtonClicked(View view)
{
    String inputText=buckysInput.getText().toString();
    dbHandler.deleteProduct(inputText);
    printDatabase();
}

 public void printDatabase(View v){
String dbString =dbHandler.databaseToString();
buckysText.setText(dbString);
buckysInput.setText("");
}

}



public class Products {
private int _id;
private String _productname;


public Products(String productname) {
    this._productname = productname;
}

public void set_id(int _id) {
    this._id = _id;
}

public void set_productname(String _productname) {
    this._productname =_productname;
}

public int get_id() {
    return _id;
}

public String get_productname() {
    return _productname;
}

}

MyDbHandler.java

public class MyDBHandler extends SQLiteOpenHelper {

private static final int DATAVASE_VERSION=2;
private static final String DATABASE_NAME="products.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, name, factory, version);
}

@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);
}
public void addProduct(Products product){
  ContentValues values=new ContentValues();
    values.put(COLUMN_PRODUCTNAME, product.get_productname());
    SQLiteDatabase db=getWritableDatabase();
    db.insert(TABLE_PRODUCTS, null, values);
    db.close();
}
public void deleteProduct(String productName)
{
    SQLiteDatabase db = getWritableDatabase();
    db.execSQL("DELETE FROM " + TABLE_PRODUCTS + " WHERE " + COLUMN_PRODUCTNAME + "=\"" + productName + "\";");
}
public String databaseToString(){
    String dbString ="";
    SQLiteDatabase db= getWritableDatabase();
    String query= "SELECT * FROM "+ TABLE_PRODUCTS+ "WHERE 1";

    Cursor c=db.rawQuery(query,null);
    c.moveToFirst();
    while(!c.isAfterLast())
    {
        if(c.getString(c.getColumnIndex("productname"))!=null)
        {
            dbString +=c.getString(c.getColumnIndex("productname"));
            dbString += "\n";

        }
    }
    db.close();
    return dbString;
}

}

3 个答案:

答案 0 :(得分:6)

缺少逗号,

CREATE TABLE products(_id INTEGER PRIMARY KEY AUTOINCREMENT, productname TEXT )
                                                           ^

答案 1 :(得分:0)

使用此更改您的查询..

 @Override
 public void onCreate(SQLiteDatabase db) {
String query="CREATE TABLE " + TABLE_PRODUCTS + "(" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_PRODUCTNAME + " TEXT " +
")";
db.execSQL(query);
}

答案 2 :(得分:0)

https://stackoverflow.com/a/41327975/5208657这对我有用。 AUTO INCREMENT语句后缺少逗号,导致错误。但是,在将产品信息添加到sqlite数据库之后,我仍然无法成功打印结果(尽管我在监视器中没有出现任何错误)。