gpt4 book ai didi

java - 编译 : near "=": syntax error (code 1): , WHERE _id = 时出现 SQLite 异常 : UPDATE person_table SET person_name=?

转载 作者:行者123 更新时间:2023-11-29 20:56:17 25 4
gpt4 key购买 nike

我在运行时遇到这个异常:

SQLite Exception: near "=": syntax error (code 1): , while compiling: UPDATE person_table SET person_name=? WHERE _id =

下面我发布了与此相关的代码。我在下面的代码中提到了错误行 51 和第 56 行。

堆栈跟踪:

12-23 02:02:19.060: E/SQLiteLog(3011): (1) near "=": syntax error
12-23 02:02:19.060: D/AndroidRuntime(3011): Shutting down VM
12-23 02:02:19.060: W/dalvikvm(3011): threadid=1: thread exiting with uncaught exception (group=0xb3a8bba8)
12-23 02:02:19.100: E/AndroidRuntime(3011): FATAL EXCEPTION: main
12-23 02:02:19.100: E/AndroidRuntime(3011): Process: com.android.timesheet, PID: 3011
12-23 02:02:19.100: E/AndroidRuntime(3011): android.database.sqlite.SQLiteException: near "=": syntax error (code 1): , while compiling: UPDATE person_table SET person_name=? WHERE _id =
12-23 02:02:19.100: E/AndroidRuntime(3011): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
12-23 02:02:19.100: E/AndroidRuntime(3011): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
12-23 02:02:19.100: E/AndroidRuntime(3011): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
12-23 02:02:19.100: E/AndroidRuntime(3011): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
12-23 02:02:19.100: E/AndroidRuntime(3011): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
12-23 02:02:19.100: E/AndroidRuntime(3011): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
12-23 02:02:19.100: E/AndroidRuntime(3011): at android.database.sqlite.SQLiteDatabase.updateWithOnConflict(SQLiteDatabase.java:1572)
12-23 02:02:19.100: E/AndroidRuntime(3011): at android.database.sqlite.SQLiteDatabase.update(SQLiteDatabase.java:1520)
12-23 02:02:19.100: E/AndroidRuntime(3011): at com.android.timesheet.database.PersonDatabaseHelper.updateData(PersonDatabaseHelper.java:51)
12-23 02:02:19.100: E/AndroidRuntime(3011): at com.android.timesheet.ModifyMember.onClick(ModifyMember.java:56)
12-23 02:02:19.100: E/AndroidRuntime(3011): at android.view.View.performClick(View.java:4438)
12-23 02:02:19.100: E/AndroidRuntime(3011): at android.view.View$PerformClick.run(View.java:18422)
12-23 02:02:19.100: E/AndroidRuntime(3011): at android.os.Handler.handleCallback(Handler.java:733)
12-23 02:02:19.100: E/AndroidRuntime(3011): at android.os.Handler.dispatchMessage(Handler.java:95)
12-23 02:02:19.100: E/AndroidRuntime(3011): at android.os.Looper.loop(Looper.java:136)
12-23 02:02:19.100: E/AndroidRuntime(3011): at android.app.ActivityThread.main(ActivityThread.java:5017)
12-23 02:02:19.100: E/AndroidRuntime(3011): at java.lang.reflect.Method.invokeNative(Native Method)
12-23 02:02:19.100: E/AndroidRuntime(3011): at java.lang.reflect.Method.invoke(Method.java:515)
12-23 02:02:19.100: E/AndroidRuntime(3011): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
12-23 02:02:19.100: E/AndroidRuntime(3011): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
12-23 02:02:19.100: E/AndroidRuntime(3011): at dalvik.system.NativeStart.main(Native Method)

PersonDatabaseHelper.java:

public class PersonDatabaseHelper {

private static final String TAG = PersonDatabaseHelper.class.getSimpleName();

// database configuration
// if you want the onUpgrade to run then change the database_version
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "mydatabase.db";

// table configuration
private static final String TABLE_NAME = "person_table"; // Table name
private static final String PERSON_TABLE_COLUMN_ID = "_id"; // a column named "_id" is required for cursor
private static final String PERSON_TABLE_COLUMN_NAME = "person_name";
private static final String PERSON_TABLE_COLUMN_PIN = "person_pin";

private DatabaseOpenHelper openHelper;
private SQLiteDatabase database;

// this is a wrapper class. that means, from outside world, anyone will communicate with PersonDatabaseHelper,
// but under the hood actually DatabaseOpenHelper class will perform database CRUD operations
public PersonDatabaseHelper(Context aContext) {

openHelper = new DatabaseOpenHelper(aContext);
database = openHelper.getWritableDatabase();
}

public void insertData (String aPersonName, String aPersonPin) {

// we are using ContentValues to avoid sql format errors

ContentValues contentValues = new ContentValues();

contentValues.put(PERSON_TABLE_COLUMN_NAME, aPersonName);
contentValues.put(PERSON_TABLE_COLUMN_PIN, aPersonPin);

database.insert(TABLE_NAME, null, contentValues);
}

public int updateData(String member_id, String memberName) {
ContentValues cvUpdate = new ContentValues();
cvUpdate.put(PersonDatabaseHelper.PERSON_TABLE_COLUMN_NAME, memberName);
int i = database.update(PersonDatabaseHelper.TABLE_NAME, cvUpdate, --->51th line Error
PersonDatabaseHelper.PERSON_TABLE_COLUMN_ID + " = " + member_id, null);
return i;
}

public void deleteData(String memberID) {
database.delete(PersonDatabaseHelper.TABLE_NAME, PersonDatabaseHelper.PERSON_TABLE_COLUMN_ID + "="
+ memberID, null);
}


public Cursor getAllData () {

String buildSQL = "SELECT * FROM " + TABLE_NAME;

Log.d(TAG, "getAllData SQL: " + buildSQL);

return database.rawQuery(buildSQL, null);
}

// this DatabaseOpenHelper class will actually be used to perform database related operation

private class DatabaseOpenHelper extends SQLiteOpenHelper {

public DatabaseOpenHelper(Context aContext) {
super(aContext, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
// Create your tables here

String buildSQL = "CREATE TABLE " + TABLE_NAME + "( " + PERSON_TABLE_COLUMN_ID + " INTEGER PRIMARY KEY, " +
PERSON_TABLE_COLUMN_NAME + " TEXT, " + PERSON_TABLE_COLUMN_PIN + " TEXT )";

Log.d(TAG, "onCreate SQL: " + buildSQL);

sqLiteDatabase.execSQL(buildSQL);
}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
// Database schema upgrade code goes here

String buildSQL = "DROP TABLE IF EXISTS " + TABLE_NAME;

Log.d(TAG, "onUpgrade SQL: " + buildSQL);

sqLiteDatabase.execSQL(buildSQL); // drop previous table

onCreate(sqLiteDatabase); // create the table from the beginning
}
}
}

ModifyMember.java:

public class ModifyMember extends Activity implements OnClickListener {

EditText et;
Button edit_bt, delete_bt;

String member_id;

PersonDatabaseHelper dbcon;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.modify_member);

dbcon = new PersonDatabaseHelper(getApplicationContext());
// dbcon.open();

et = (EditText) findViewById(R.id.edit_mem_id);
edit_bt = (Button) findViewById(R.id.update_bt_id);
delete_bt = (Button) findViewById(R.id.delete_bt_id);

Intent i = getIntent();
String memberID = i.getStringExtra("memberID");
String memberName = i.getStringExtra("memberName");

member_id=memberID;
// member_id = Long.parseLong(memberID);

et.setText(memberName);

edit_bt.setOnClickListener(this);
delete_bt.setOnClickListener(this);

}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.update_bt_id:
String memName_upd = et.getText().toString();
dbcon.updateData(member_id, memName_upd); ----> 56th line error
this.returnHome();
break;

case R.id.delete_bt_id:

dbcon.deleteData(member_id);
this.returnHome();
break;
}
}

public void returnHome() {

Intent home_intent = new Intent(getApplicationContext(),
NewProjects.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

startActivity(home_intent);
}

}

任何人都可以帮我解决这个问题。谢谢。

最佳答案

改变这个

    database.update(PersonDatabaseHelper.TABLE_NAME, cvUpdate, --->51th line Error
PersonDatabaseHelper.PERSON_TABLE_COLUMN_ID + " = " + member_id, null);

进入

    database.update(PersonDatabaseHelper.TABLE_NAME, cvUpdate, 
PersonDatabaseHelper.PERSON_TABLE_COLUMN_ID + " =?" , new String[]{member_id});

关于java - 编译 : near "=": syntax error (code 1): , WHERE _id = 时出现 SQLite 异常 : UPDATE person_table SET person_name=?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27616100/

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