我有一个像这样创建的数据库......
public class DataBaseManager extends SQLiteOpenHelper{
Context mContext;
private static final String TAG = "DataBaseManager";
private static final int dbVersion = 1;// ++ for DB changes
static final String dbName ="LCInstore";
//Table Names
static final String allIcons = "Icons";
static final String allScreens = "Screens";
static final String isLookUp = "LookUp";
//Column Names - LookUp
static final String colIconID = "IconID";
static final String colScreenID = "ScreenID";
static final String colRank = "Rank"; // order
//Column Names shared by tables: Screens and Icons
static final String colID = "ID";
static final String colType = "Type";
static final String colName = "Name";
//Column Names - Icons
static final String colImage = "Image";
static final String colLabel = "Label";
static final String colIntent = "Intent";
static final String colIParams = "Params";
public DataBaseManager(Context context) {
super(context, dbName, null, dbVersion);
mContext = context;
Log.v(TAG, "Initaited");
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
Log.v(TAG, "on create called");
// Create Icon Table if does not exist
db.execSQL("CREATE TABLE "+ allIcons +"" +
"("+colID + " INTEGER PRIMARY KEY AUTOINCREMENT, "+
colName + " TEXT," +
colImage + " BLOB," +
colLabel + " TEXT," +
colIntent + " TEXT," +
colType + " TEXT)");
// Create Screens Table if does not exist
db.execSQL("CREATE TABLE IF NOT EXISTS " + allScreens +"" +
"("+colID + " INTEGER PRIMARY KEY AUTOINCREMENT, "+
colName + " TEXT," +
colType + " TEXT)");
//Create LookUp Table if does not exist
db.execSQL("CREATE TABLE IF NOT EXISTS " + isLookUp +"" +
"("+colIconID + " INTEGER, "+
colScreenID + " INTEGER," +
colRank + " INTEGER)");
InsertInitData(db);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
Log.v(TAG, "on upgrade called");
//db.execSQL("DROP TABLE IF EXISTS "+allIcons);
}
private void InsertInitData(SQLiteDatabase db) {
//screens
ContentValues cv2=new ContentValues();
cv2.put(colName, "DR_Home");
cv2.put(colType, "dr_home");
db.insert(allScreens, colID, cv2);
cv2.put(colName, "DR_Sub");
cv2.put(colType, "dr_sub");
db.insert(allScreens, colID, cv2);
//all Icons
ContentValues cv=new ContentValues();
cv.put(colName, "Icon1");
cv.put(colImage, getBlobFromRes(R.drawable.dr_tablet_icon));
cv.put(colLabel, "MY ICON 1");
cv.put(colIntent, "someIntent");
cv.put(colType, "generic");
db.insert(allIcons, colID, cv);
cv.put(colName, "Icon2");
cv.put(colImage, getBlobFromRes(R.drawable.dr_tablet_icon));
cv.put(colLabel, "MY ICON 2");
cv.put(colIntent, "someIntent");
cv.put(colType, "generic");
db.insert(allIcons, colID, cv);
cv.put(colName, "Icon3");
cv.put(colImage, getBlobFromRes(R.drawable.dr_tablet_icon));
cv.put(colLabel, "MY ICON 3");
cv.put(colIntent, "someIntent");
cv.put(colType, "generic");
db.insert(allIcons, colID, cv);
cv.put(colName, "Icon4");
cv.put(colImage, getBlobFromRes(R.drawable.dr_tablet_icon));
cv.put(colLabel, "MY ICON 4");
cv.put(colIntent, "someIntent");
cv.put(colType, "generic");
db.insert(allIcons, colID, cv);
cv.put(colName, "Icon5");
cv.put(colImage, getBlobFromRes(R.drawable.dr_tablet_icon));
cv.put(colLabel, "MY ICON 5");
cv.put(colIntent, "someIntent");
cv.put(colType, "generic");
db.insert(allIcons, colID, cv);
cv.put(colName, "Icon6");
cv.put(colImage, getBlobFromRes(R.drawable.dr_tablet_icon));
cv.put(colLabel, "MY ICON 6");
cv.put(colIntent, "someIntent");
cv.put(colType, "generic");
db.insert(allIcons, colID, cv);
cv.put(colName, "Icon7");
cv.put(colImage, getBlobFromRes(R.drawable.dr_tablet_icon));
cv.put(colLabel, "MY ICON 7");
cv.put(colIntent, "someIntent");
cv.put(colType, "generic");
db.insert(allIcons, colID, cv);
// icon screen lookups
ContentValues cv3=new ContentValues();
cv3.put(colIconID, 1);
cv3.put(colScreenID, 2);
cv3.put(colRank, 1);
db.insert(isLookUp, colID, cv3);
cv3.put(colIconID, 2);
cv3.put(colScreenID, 2);
cv3.put(colRank, 2);
db.insert(isLookUp, colID, cv3);
cv3.put(colIconID, 3);
cv3.put(colScreenID, 2);
cv3.put(colRank, 3);
db.insert(isLookUp, colID, cv3);
cv3.put(colIconID, 4);
cv3.put(colScreenID, 2);
cv3.put(colRank, 4);
db.insert(isLookUp, colID, cv3);
cv3.put(colIconID, 5);
cv3.put(colScreenID, 2);
cv3.put(colRank, 5);
db.insert(isLookUp, colID, cv3);
cv3.put(colIconID, 6);
cv3.put(colScreenID, 2);
cv3.put(colRank, 6);
db.insert(isLookUp, colID, cv3);
cv3.put(colIconID, 7);
cv3.put(colScreenID, 2);
cv3.put(colRank, 7);
db.insert(isLookUp, colID, cv3);
}
你会注意到一个表,'allScreens'或“Screens”有一行在Name列中我放“DR_Home”
我稍后会使用此方法查找该行的ID列的值并将DR_Home
传递给它:
public int getScreenID(String name){
SQLiteDatabase db=this.getReadableDatabase();
int sID;
String selectQuery = "SELECT "+colID+ " FROM " + allScreens + " WHERE " + colName + "=" +name;
Cursor c = db.rawQuery(selectQuery, null);
sID = c.getInt(0);
c.close();
return sID;
}
应该返回ID列的值但是我收到错误:
03-22 06:34:35.830: E/AndroidRuntime(29864): Caused by: android.database.sqlite.SQLiteException: no such column: DR_Home: , while compiling: select * from Screens WHERE Name=DR_Home
没有这样的专栏?我没有要求一个名为DR_Home的列我要求名称列= DR_home的行中的ID列的值...至少这就是我的想法..我搞砸了某处的语法..请帮助
答案 0 :(得分:2)
您的查询错误,因为WHERE column = string
无效。
String selectQuery = "SELECT "+colID+ " FROM " + allScreens + " WHERE " + colName + "=?";
Cursor c = db.rawQuery(selectQuery, new String[] {name});
使用?
,它将替换为第二个rawQuery()
参数。强烈推荐。
不推荐:WHERE column = "string"
答案 1 :(得分:2)
这样做:
String selectQuery = String.format("SELECT %s FROM %s WHERE %s = ?",
colID, allScreens, colName);
Cursor c = db.rawQuery(selectQuery, new String[]{name});
答案 2 :(得分:0)
将旧查询"SELECT "+colID+ " FROM " + allScreens + " WHERE " + colName + "=" +name;
更改为
"SELECT "+colID+ " FROM " + allScreens + " WHERE " + colName + "='" +name+"'";
答案 3 :(得分:-1)
name = DR_home
表示“列名等于列DR_home
。如果你想要字符串搜索,你可能想引用它(我推测你这样做,但我没有深入窥探你的代码它太长了):name = 'DR_Home'
。
答案 4 :(得分:-1)
您的查询字符串错误
String selectQuery = "SELECT "+colID+ " FROM " + allScreens + " WHERE " + colName + " = '" +name + "'";
您在哪里提供的名称应该是单引号