gpt4 book ai didi

android - 没有这样的列错误 - sqlite

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

我得到一个奇怪的错误:

E/SQLiteLog﹕ (1) no such column: Test
06-22 01:51:20.006 15089-15089/com.almas.mehr.sms E/AndroidRuntime﹕ FATAL EXCEPTION: main
android.database.sqlite.SQLiteException: no such column: Test (code 1): , while compiling: SELECT * FROM groups WHERE title = Test
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1090)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:663)
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:1420)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1359)
at com.almas.mehr.databases.MainDB.isSameGroup(MainDB.java:93)
at com.almas.mehr.submain.Groups.onClick(Groups.java:99)
at android.view.View.performClick(View.java:4354)

数据库类:

//database name
private final static String DATABASE_NAME = "almasmehrsms" ;

//tables name
private final String GROUPS = "groups";
private final String NOTIFICATIONS = "notifications";

//columns of groups table
private final String ID_GROUPS = "_id";
private final String TITLE_GROUPS = "title";
private final String DATE_GROUPS = "gdate";

//create groups table
private final String CREATE_GROUPS_TABLE = "CREATE TABLE " + GROUPS + "("
+ ID_GROUPS + " INTEGER PRIMARY KEY AUTOINCREMENT," + TITLE_GROUPS + " TEXT," +
DATE_GROUPS + " TIMESTAMP DEFAULT CURRENT_TIMESTAMP" + ")";



public MainDB(Context context) {
super(context, DATABASE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_GROUPS_TABLE);

}

@Override
public void onUpgrade(SQLiteDatabase db, int i, int i2) {
db.execSQL("DROP TABLE IF EXISTS " + GROUPS);
db.execSQL("DROP TABLE IF EXISTS " + NOTIFICATIONS);
onCreate(db);
}


public Boolean isSameGroup(String title){
Boolean issamegroup = true ;
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT * FROM "+ GROUPS +" WHERE "+ TITLE_GROUPS +" = " + title;
Cursor cursor = db.rawQuery(query, null);
cursor.moveToFirst();
if(cursor.getCount() > 0) {
issamegroup = true ;
}else{

issamegroup = false ;
}

return issamegroup ;
}

我写了这个函数来检查一个值是否重复:

public Boolean isSameGroup(String title){

...

}

例如,我从 EditText 中输入的是 Test,我收到此消息错误:

 E/SQLiteLog﹕ (1) no such column: Test

最佳答案

SQL 中的字符串文字用单引号 (') 表示。没有它们,字符串将被视为对象名称。在这里,您生成一个 where 子句 title = Test。两者都被解释为列名,并且查询失败,因为没有列 Test

要解决这个问题,您可以用引号将 Test 括起来:

String query = "SELECT * FROM "+ GROUPS +" WHERE "+ TITLE_GROUPS + " = '" + title + "'";

关于android - 没有这样的列错误 - sqlite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30969531/

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