gpt4 book ai didi

java - SQLite项目中的语法bug,找不到他

转载 作者:行者123 更新时间:2023-12-02 12:29:56 41 4
gpt4 key购买 nike

我首先尝试使用 SQLite,我有语法错误,但我找不到他 5-6 小时

来自MainActivity.java的代码,在这个类中我调用数据库:

package ua.bellkross.notepadjava;

import android.content.ContentValues;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

EditText editText;
DatabaseHelper databaseHelper;
SQLiteDatabase db;
Cursor userCursor;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

editText = (EditText) findViewById(R.id.editText);

databaseHelper = new DatabaseHelper(getApplicationContext());

}

@Override
protected void onResume() {
super.onResume();
db = databaseHelper.getWritableDatabase();

userCursor = db.rawQuery("select * from " + DatabaseHelper.TABLE, null);
editText.setText(userCursor.getString(userCursor.getColumnIndex(DatabaseHelper.COLUMN_NAME)));
}

}

来自DatabaseHelper.java的代码,在这段代码中我有语法错误,在方法onCreate中:

package ua.bellkross.notepadjava;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "userstore.db"; // название бд
private static final int SCHEMA = 1; // версия базы данных
static final String TABLE = "table"; // название таблицы в бд
// названия столбцов
public static final String COLUMN_ID = "_id";
public static final String COLUMN_NAME = "name";

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

@Override
public void onCreate(SQLiteDatabase db) {

db.execSQL("CREATE TABLE users (" + COLUMN_ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_NAME + " TEXT);");
// добавление начальных данных
db.execSQL("INSERT INTO "+ TABLE +"(" + COLUMN_NAME + ") VALUES ('Text');");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+TABLE);
onCreate(db);
}
}

这是来自 logcat 的消息:

Caused by: android.database.sqlite.SQLiteException: near "table": syntax error (code 1): , while compiling: INSERT INTO table(name) VALUES ('Text'); 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.(SQLiteProgram.java:58) at android.database.sqlite.SQLiteStatement.(SQLiteStatement.java:31) at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1677) at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1608) at ua.bellkross.notepadjava.DatabaseHelper.onCreate(DatabaseHelper.java:25) at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251) at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163) at ua.bellkross.notepadjava.MainActivity.onResume(MainActivity.java:33) at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1269) at android.app.Activity.performResume(Activity.java:6766) at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3377) at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3440)  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2713)  at android.app.ActivityThread.-wrap12(ActivityThread.java)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:154)  at android.app.ActivityThread.main(ActivityThread.java:6077)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)  07-26 10:03:09.757 1375-1382/ua.bellkross.notepadjava W/art: Suspending all threads took: 8.571ms

最佳答案

您正在使用table作为插入的表名称,因此请在插入语句中更改表名称

static final String TABLE = "users"

关于java - SQLite项目中的语法bug,找不到他,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45327112/

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