我正在使用Sqlite数据库并为行存储空值。我正在使用此代码在数据库中存储空值:
ContentValues values = new ContentValues();
values.put(KEY_NAME,"");
values.put(KEY_PH_NO,"");
values.put(KEY_FOLDER_NAME, contact.getFolderName());
values.put(KEY_FolderAddress, contact.getParentFolderAddress());
values.put(KEY_TIME, contact.getTime());
values.put(KEY_DATE, contact.getDate());
我正在使用以下查询来检索具有空值的数据库条目:
String selectQuery = "SELECT * FROM " + TABLE_CONTACTS + " WHERE "+KEY_FolderAddress+" ='"
+FileAddress+"'"+" AND "+KEY_FOLDER_NAME+" ='"+FileName+"'"+" AND "+KEY_DATE+" ='"+"0"+"'"
+" AND "+KEY_PH_NO+" ='"+""+"'"+" AND "+KEY_NAME+" ='"+""+"'";
但我在检索空值时遇到此异常:
12-25 11:52:40.531: E/Exception:(1990): android.database.sqlite.SQLiteException: unknown error: , while compiling: SELECT * FROM rules WHERE folderaddress ='/sdcard/Velosys/a' AND folder_name ='a' AND date ='0' AND phone_number ='' AND name ='' Exception occured in getTimeOfTheFolderFromContact() of DatabaseHandlerRule.java
我已经搜索了很多但没有得到解决方案。请帮我找到从sqlite数据库中检索空值的正确方法。提前谢谢
编辑:
I am storing 2 entries with empty values i.e KEY_NAME and KEY_PH_NO
这是表格格式:
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
+ KEY_ID + " INTEGER PRIMARY KEY, " +KEY_NAME+" TEXT, "+ KEY_PH_NO + " TEXT, "
+KEY_FOLDER_NAME+" TEXT, "+ KEY_FolderAddress + " TEXT, "+KEY_TIME + " TEXT, "+KEY_DATE+" TEXT "+")";
db.execSQL(CREATE_CONTACTS_TABLE);
答案 0 :(得分:1)
您必须使用一些常用值来存储在数据库中:
ContentValues values = new ContentValues();
values.put(KEY_NAME,"null");
values.put(KEY_PH_NO,"null");
请使用此代码检索空值: 是的,你是对的,因为“null”值将作为字符串值存储在数据库中。
int i = resultSet.getInt(resultSet.getColumnIndex(columnName));
String str = resultSet.getString(resultSet.getColumnIndex(columnName));
boolean bool = resultSet.isNull(resultSet.getColumnIndex(columnName));
if( str == null )
{
return -1;
}
return i;
答案 1 :(得分:1)
对于插入任何值,您的方法方法是正确的。 例如:
public boolean insertSample() {
boolean success = false;
ContentValues values = new ContentValues();
values.put(KEY_NAME, "");
values.put(KEY_PH_NO, "");
values.put(KEY_FOLDER_NAME, "foldername");
values.put(KEY_FolderAddress, "folderAddress");
values.put(KEY_TIME, "anytime");
values.put(KEY_DATE, "date");
long id = database.insert(TABLE_SAMPLe_DETAIL, null, values);
success = id > 0;
return success;
}
对于检索你可以这样使用。
public Cursor getAllSample(String fileAddress, String fileName,
String keyDate, String keyPhone, String keyName) {
String whereClause = KEY_FolderAddress + "=? AND " + KEY_FOLDER_NAME
+ " =? AND " + KEY_DATE + " =? AND " + KEY_PH_NO + " =? AND "
+ KEY_NAME + " =?";
String[] whereArgs = new String[] { String.valueOf(fileAddress),
String.valueOf(fileName), String.valueOf(keyDate),
String.valueOf(keyPhone), String.valueOf(keyName) };
Cursor cursor = database.query(TABLE_SAMPLe_DETAIL, null, whereClause, whereArgs,
null, null, null);
if(cursor != null && cursor.getCount() > 0) {
System.out.println("Your Query is correct");
}
return cursor;
}
您可以将此方法称为: getAllSample(“folderAddress”,“foldername”,“date”,“”,“”);
其中database是SQLiteDatabase的对象。
答案 2 :(得分:1)
尝试将这两个值保存为String
values.put(KEY_TIME, contact.getTime());
values.put(KEY_DATE, contact.getDate());
答案 3 :(得分:0)
在运行之前,每个sql查询最好在Sqliteman或任何sqlite查询浏览器上运行,这有两个好处:
1-您看到语法错误并可以修复它们。
2-你知道列的默认值
因此,如果null存储在列中,则可以在查询的where子句中使用“IS NULL”。