我想在将数据添加到表之前确定studentId与另一个studentId不一样但我不知道如何使用光标搜索长类型
我的代码:
public void SaveStudent(Long studentId , int classID , String studentName)
{
String stdId = studentId.toString();
Cursor cursor = database.rawQuery("select * from student where student_id = ?",
new String[]{stdId});
if (cursor.getCount() == 0)
{
ContentValues values = new ContentValues();
values.put("student_id", studentId);
values.put("class_id", classID);
values.put("student_name", studentName);
database.insert("student", null, values);
Toast.makeText(context, "save!", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(context, "StudentID is duplicate", Toast.LENGTH_SHORT).show();
}
}
logcat错误:
02-23 21:37:39.108 26986-26986/com.example.user.classmanager E/SQLiteLog: (1) near "=": syntax error
02-23 21:37:39.110 26986-26986/com.example.user.classmanager E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.user.classmanager, PID: 26986
android.database.sqlite.SQLiteException: near "=": syntax error (code 1): , while compiling: select * from studentwhere student_id = ?
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
at com.example.user.classmanager.DatabaseHandler.SaveStudent(DatabaseHandler.java:184)
at com.example.user.classmanager.SecondTab$1$1$1.onClick(SecondTab.java:97)
答案 0 :(得分:1)
尝试以下
Cursor cursor = database.rawQuery("select * from student where student_id = ?",
new String[]{String.valueOf(stdId)});