gpt4 book ai didi

java - 创建表时出现 Android SqLite 异常

转载 作者:行者123 更新时间:2023-12-01 00:18:41 26 4
gpt4 key购买 nike

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

主 Activity .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;
}

最佳答案

逗号 , 丢失:

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

关于java - 创建表时出现 Android SqLite 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41327924/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com