我为转换器创建此数据库。它就像一个二维数组。如果我没有误会,我应该看起来像图片(不是与代码中的顺序相同)。
现在"getData(String x, String y)"
方法应该到数据库的x行返回y列中的值。例如。根据PICTURE,如果我拨打"getData("CAD", "EUR")"
,该方法应该返回"0.64955"
。
但应用程序崩溃了。 logcat的:
07-23 06:15:10.925: E/AndroidRuntime(28756): FATAL EXCEPTION: main
07-23 06:15:10.925: E/AndroidRuntime(28756): android.database.sqlite.SQLiteException: near "table": syntax error (code 1): , while compiling: SELECT id, AUD, CAD, EUR, GBP, USD FROM table
public class database {
private Db DbHelper;
private Context ct;
private SQLiteDatabase database;
private static class Db extends SQLiteOpenHelper{
public Db(Context context) {
super(context, "db", null, 1);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
String query;
query = "CREATE TABLE table (null, AUD, CAD, EUR, GBP, USD)";
db.execSQL(query);
db.execSQL("INSERT INTO AUD VALUES (AUD, 1, 1.00074, 0.65018, 0.54310, 0.90261)");
db.execSQL("INSERT INTO CAD VALUES (CAD, 0.99888, 1, 0.64955, 0.54259, 0.90177)");
db.execSQL("INSERT INTO EUR VALUES (EUR, 1.53774, 1.53910, 1, 0.83528, 1.38818)");
db.execSQL("INSERT INTO GBP VALUES (GBP, 1.84069, 1.84240, 1.19697, 1, 1.66174)");
db.execSQL("INSERT INTO USD VALUES (USD, 1.10769, 1.10872, 0.72030, 0.60170, 1)");
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}}
public database(Context c){
ct = c;
}
public database open(){
DbHelper = new Db(ct);
database = DbHelper.getWritableDatabase();
return this;
}
public void close(){
DbHelper.close();
}
public double getData(String x, String y) {
// TODO Auto-generated method stub
String [] col = new String[]{"id", "AUD", "CAD", "EUR", "GBP", "USD"};
Cursor c = database.query("table", col, null, null, null, null, null);
int iRow = c.getColumnIndex(y);
String result = "";
for(c.moveToFirst();!c.isAfterLast();c.moveToNext()){
result = c.getString(iRow);
}
return result;
}
}
答案 0 :(得分:0)
您创建了一个名为“table”的表,但字符串“table”是SQL keyword;尝试改变它:
query = "CREATE TABLE my_table (null, AUD, CAD, EUR, GBP, USD)";
答案 1 :(得分:0)
代码中的问题。
所以进行所有更改后,您的查询应如下所示:
query = "CREATE TABLE currency_tab (id INTEGER PRIMARY KEY AUTOINCREMENT, currency_name TEXT NOT NULL, AUD TEXT NOT NULL, CAD TEXT NOT NULL, EUR TEXT NOT NULL, GBP TEXT NOT NULL, USD TEXT NOT NULL)";