因此,我一直在尝试使用getData()
方法进行查询,并且每当我运行debug时,我都可以看到它在dbHelper.getWritableDatabase()
行中崩溃了。
我以为我是通过onCreate()
方法创建数据库的,所以我不确定发生了什么。
我的代码在下面。我将不胜感激!
public String getData(int firstSelection, int secondSelection, int thirdSelection,
int fourthSelection, int fifthSelection)
{
SQLiteDatabase db = dbHelper.getWritableDatabase();
String firstSelectionStr, secondSelectionStr, thirdSelectionStr, fourthSelectionStr, fifthSelectionStr;
firstSelectionStr = Integer.toString(firstSelection);
secondSelectionStr = Integer.toString(secondSelection);
thirdSelectionStr = Integer.toString(thirdSelection);
fourthSelectionStr = Integer.toString(fourthSelection);
fifthSelectionStr = Integer.toString(fifthSelection);
String[] columns = {DBHelper.UID,DBHelper.CNAME};
String selectQuery = "SELECT " + DBHelper.CNAME + " FROM "+ "CraftsAppDatabase" + " WHERE First_Attribute=? "
+ " AND Second_Attribute=? " + " AND Third_Attribute=? " + " AND Fourth_Attribute=? "
+ " AND Fifth_Attribute=? ";
Cursor cursor=db.rawQuery(selectQuery, new String[] {firstSelectionStr, secondSelectionStr, thirdSelectionStr,
fourthSelectionStr, fifthSelectionStr});
StringBuilder buffer = new StringBuilder();
// Append every data together
while (cursor.moveToNext())
{
String chosenItem = cursor.getString(cursor.getColumnIndex(DBHelper.CNAME));
buffer.append(chosenItem + "/n");
}
return buffer.toString();
}
static class DBHelper extends SQLiteOpenHelper
{
private static final String DATABASE_NAME = "CraftsAppDatabase"; // Database Name
private static final String TABLE_NAME = "CraftTools"; // Table Name
private static final String RESULT_TABLE = "Result"; // Table Name
private static final int DATABASE_Version = 1; // Database Version
private static final String UID="_id"; // Column I (Primary Key)
private static final String CNAME = "Craft_Name"; //Column II
private static final String RESULT = "Result_Name"; //Column II
private static final String FIRST_ATTRIBUTE = "First_Attribute"; //Column III
private static final String SECOND_ATTRIBUTE = "Second_Attribute"; //Column IV
private static final String THIRD_ATTRIBUTE = "Third_Attribute"; //Column V
private static final String FOURTH_ATTRIBUTE = "Fourth_Attribute"; //Column VI
private static final String FIFTH_ATTRIBUTE = "Fifth_Attribute"; //Column VII
private static final String CREATE_TABLE = "CREATE TABLE "+TABLE_NAME+
" ("+UID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+CNAME+" VARCHAR(255)" +
", "+FIRST_ATTRIBUTE+" VARCHAR(255), "+SECOND_ATTRIBUTE+" VARCHAR(255)" +
", "+THIRD_ATTRIBUTE+" VARCHAR(255), "+FOURTH_ATTRIBUTE+" VARCHAR(255)" +
", "+FIFTH_ATTRIBUTE+" VARCHAR(255));";
private static final String CREATE_OTHER_TABLE = "CREATE TABLE "+RESULT_TABLE+
" ("+UID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+RESULT+" VARCHAR(255));";
private static final String DROP_TABLE ="DROP TABLE IF EXISTS "+TABLE_NAME;
private Context context;
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_Version);
this.context=context;
}
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE);
db.execSQL(CREATE_OTHER_TABLE);
db.execSQL("INSERT INTO " + TABLE_NAME + "(CNAME, First_Attribute, Second_Attribute, Third_Attribute, Fourth_Attribute, Fifth_Attribute ) " +
"VALUES ('Landscape Drawing', '1', '4','8', 'NONE', 'NONE')");
db.execSQL("INSERT INTO " + TABLE_NAME + "(CNAME, First_Attribute, Second_Attribute, Third_Attribute, Fourth_Attribute, Fifth_Attribute ) " +
"VALUES ('Popsicle Sticks House', '2', '3','NONE', 'NONE', 'NONE')");
db.execSQL("INSERT INTO " + TABLE_NAME + "(CNAME, First_Attribute, Second_Attribute, Third_Attribute, Fourth_Attribute, Fifth_Attribute ) " +
"VALUES ('Sunset Painting', '4', '7','10', 'NONE', 'NONE')");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(DROP_TABLE);
onCreate(db);
if (newVersion > oldVersion) {
db.execSQL("ALTER TABLE CraftTools ADD COLUMN FIRST_ATTRIBUTE INTEGER DEFAULT 0");
db.execSQL("ALTER TABLE CraftTools ADD COLUMN SECOND_ATTRIBUTE INTEGER DEFAULT 0");
db.execSQL("ALTER TABLE CraftTools ADD COLUMN THIRD_ATTRIBUTE INTEGER DEFAULT 0");
db.execSQL("ALTER TABLE CraftTools ADD COLUMN FOURTH_ATTRIBUTE INTEGER DEFAULT 0");
db.execSQL("ALTER TABLE CraftTools ADD COLUMN FIFTH_ATTRIBUTE INTEGER DEFAULT 0");
}
}
答案 0 :(得分:0)
DATABASE_NAME没有.db扩展名。那可能是个问题。 由于未初始化dbHelper,可能会引起另一个问题。
答案 1 :(得分:0)
这是获取句柄和查询的方法:
public DBHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "crafts.db";
private static final String TABLE_CRAFT_TOOLS = "craft_tools";
private static final String KEY_CRAFT_NAME = "craft_name";
protected static SQLiteDatabase db = null;
/** Constructor */
public SqliteBaseHelper(Context context, String name, CursorFactory factory, int version) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
if(db == null) {db = getWritableDatabase();}
}
/** this method possibly belongs into the helper class */
public String getString(int firstSelection, int secondSelection, int thirdSelection, int fourthSelection, int fifthSelection) {
StringBuilder sb = new StringBuilder();
String sql = "SELECT " + KEY_CRAFT_NAME + " FROM " + TABLE_CRAFT_TOOLS + " WHERE " +
"First_Attribute=? AND " +
"Second_Attribute=? AND " +
"Third_Attribute=? AND " +
"Fourth_Attribute=? AND " +
"Fifth_Attribute=? ";
String[] selectionArgs = new String[] {
Integer.toString(firstSelection),
Integer.toString(secondSelection),
Integer.toString(thirdSelection),
Integer.toString(fourthSelection),
Integer.toString(fifthSelection)
};
try {
Cursor cursor = db.rawQuery(sql, selectionArgs);
if (cursor.moveToFirst()) {
do {
sb.append(cursor.getString(cursor.getColumnIndex(KEY_CRAFT_NAME)) + "/n");
} while(cursor.moveToNext());
} else {
Log.w(LOG_TAG, "no crafts found.");
}
} catch (SQLiteException e) {
Log.e(LOG_TAG, e.getMessage());
} finally {
if (! cursor.isClosed()) {
cursor.close();
}
}
return sb.toString();
}
...
}
答案 2 :(得分:0)
我终于想通了。以下命令中的{posts.map(post =>
(post.id < 2) && <Post key={post.id} title={post.title} />
}
应该是CNAME
。
Craft_Name