如何使用游标在数据库中搜索长类型
how to search long type in database with cursor
我想在向 table 添加数据之前确保 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.onClick(SecondTab.java:97)
尝试以下方法
Cursor cursor = database.rawQuery("select * from student where student_id = ?",
new String[]{String.valueOf(stdId)});
我想在向 table 添加数据之前确保 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.onClick(SecondTab.java:97)
尝试以下方法
Cursor cursor = database.rawQuery("select * from student where student_id = ?",
new String[]{String.valueOf(stdId)});