gpt4 book ai didi

android - 我无法将 textview 字符串存储到 Android 中的 Sqlite 数据库中。下面是我的 DatabaseHelper 代码

转载 作者:搜寻专家 更新时间:2023-11-01 07:51:44 25 4
gpt4 key购买 nike

DatabaeHelper.class

这是一个普通类。但我无法插入值并从数据库中取回数据。

         public class DatabaseHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "pharmabarcode.db";
private static final String TABLE_NAME = "pbstable";
private static final String KEY_ID = "id";
private static final String KEY_NAME = "result";

public DatabaseHelper(Context context){
super(context,DATABASE_NAME,null,DATABASE_VERSION);

}


@Override
public void onCreate(SQLiteDatabase db) {

db.openOrCreateDatabase("pharmabarcode.db", null);
String CREATE_TABLE = "CREATE TABLE" + TABLE_NAME + "(" + KEY_ID + "INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_NAME + "TEXT" + ")";
db.execSQL(CREATE_TABLE);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXIST"+ TABLE_NAME);
onCreate(db);

}

public boolean insertData(String result){

SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(KEY_NAME, result);
long rslt = db.insert(TABLE_NAME,null,contentValues);
if(rslt == -1)
return false;
else
return true;
}
public Cursor getAllData(){

SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from "+ TABLE_NAME,null );
return res;
}}

这是向数据库中插入值的mainactivity代码

    try {
boolean isInserted = mydb.insertData(barcodeValue.getText().toString());
if (isInserted = true)
Toast.makeText(getApplicationContext(), "Data Inserted", Toast.LENGTH_LONG).show();
else
Toast.makeText(getApplicationContext(), "Data not Inserted", Toast.LENGTH_LONG).show();
}catch (Exception e){
Log.d("Exception occures",""+e);
}

这是我的堆栈跟踪,表示没有表

    12-10 11:30:35.553 3158-3158/com.google.android.gms.samples.vision.barcodereader E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.google.android.gms.samples.vision.barcodereader, PID: 3158
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.google.android.gms.samples.vision.barcodereader/com.google.android.gms.samples.vision.barcodereader.HistoryActivity}: android.database.sqlite.SQLiteException: no such table: pbstable (code 1): , while compiling: select * from pbstable
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2493)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2555)
at android.app.ActivityThread.access$800(ActivityThread.java:176)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1437)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5576)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:956)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:751)
Caused by: android.database.sqlite.SQLiteException: no such table: pbstable (code 1): , while compiling: select * from pbstable
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:898)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:509)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1346)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1285)
at com.google.android.gms.samples.vision.barcodereader.DatabaseHelper.getAllData(DatabaseHelper.java:53)
at com.google.android.gms.samples.vision.barcodereader.HistoryActivity.onCreate(HistoryActivity.java:30)
at android.app.Activity.performCreate(Activity.java:6005)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2446)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2555) 
at android.app.ActivityThread.access$800(ActivityThread.java:176) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1437) 
at android.os.Handler.dispatchMessage(Handler.java:111) 
at android.os.Looper.loop(Looper.java:194) 
at android.app.ActivityThread.main(ActivityThread.java:5576) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:956) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:751) 

最佳答案

你的日志

.database.sqlite.SQLiteException: no such table: pbstable (code 1): , while compiling: select * from pbstable

是因为Create Tale SQL Command错误。您应该在 Column TypeColumn Name

之间添加 white space

我在此处添加了一个空格 - “CREATE TABLE”

 String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_NAME + " TEXT" + ")";

关于android - 我无法将 textview 字符串存储到 Android 中的 Sqlite 数据库中。下面是我的 DatabaseHelper 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34194845/

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