2016-09-30 68 views
-2

我的android代码正在将字符串保存到SQlite数据库,并且它只在我使用数字时才起作用。如果我使用字母,它会崩溃。当使用字母时,Android SQLite崩溃

这是代码:

final SQLiteDatabase mydatabase = openOrCreateDatabase("localdatabase",MODE_PRIVATE,null); 
mydatabase.execSQL("CREATE TABLE IF NOT EXISTS SPENT(id INTEGER PRIMARY KEY AUTOINCREMENT, date VARCHAR, value VARCHAR,label1 " + 
    "VARCHAR,label2 VARCHAR,label3 VARCHAR,label4 VARCHAR,label5 VARCHAR);"); 

String uniqueID = UUID.randomUUID().toString(); 
long unixTime = System.currentTimeMillis()/1000L; 

EditText et1 = (EditText) findViewById(R.id.editText); 
EditText et2 = (EditText) findViewById(R.id.editText2); 
EditText et3 = (EditText) findViewById(R.id.editText3); 
EditText et4 = (EditText) findViewById(R.id.editText4); 
EditText et5 = (EditText) findViewById(R.id.editText5); 
EditText et6 = (EditText) findViewById(R.id.editText6); 

String str1 = et1.getText().toString(); 
String str2 = et2.getText().toString(); 
String str3 = et3.getText().toString(); 
String str4 = et4.getText().toString(); 
String str5 = et5.getText().toString(); 
String str6 = et6.getText().toString(); 

if (str1.isEmpty()){str1 = "0";}; 
if (str2.isEmpty()){str2 = "0";}; 
if (str3.isEmpty()){str3 = "0";}; 
if (str4.isEmpty()){str4 = "0";}; 
if (str5.isEmpty()){str5 = "0";}; 
if (str6.isEmpty()){str6 = "0";}; 

String Msg = "WriteNewLoan,"+userName+","+unixTime+","+str1+","+str2+"," 
    +str3+","+str4+","+str5+","+str6+","+uniqueID+","+base64PW; 

mydatabase.execSQL("INSERT INTO LOAN VALUES("+unixTime+","+str1+","+str2 
    +","+str3+","+str4+","+str5+","+str6+");"); 

而且这是在目录下载错误:

Process: com.spendingtracker.readdeo.spendingtracker, PID: 21859 
android.database.sqlite.SQLiteException: unrecognized token: "2hvg" (code 1): , while compiling: INSERT INTO SPENT (date,value,label1,label2,label3,label4,label5) VALUES(1475224906,0,0,2hvg,0,0,0); 
    at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) 
    at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889) 
    at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500) 
    at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) 
    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58) 
    at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31) 
    at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1674) 
    at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1605) 
    at com.spendingtracker.readdeo.spendingtracker.MainActivity$4.onClick(MainActivity.java:242) 
    at android.view.View.performClick(View.java:5204) 
    at android.view.View$PerformClick.run(View.java:21156) 
    at android.os.Handler.handleCallback(Handler.java:739) 
    at android.os.Handler.dispatchMessage(Handler.java:95) 
    at android.os.Looper.loop(Looper.java:148) 
    at android.app.ActivityThread.main(ActivityThread.java:5466) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

错误指向该行:

mydatabase.execSQL("INSERT INTO SPENT (date,value,label1,label2,label3,label4,label5) VALUES("+unixTime+","+str1+","+str2 
    +","+str3+","+str4+","+str5+","+str6+");"); 

无法识别的记号“ 2hvg“是EditText中的一个字符串,如果它不包含数字,则会使其崩溃。

你能告诉我为什么这不能用字母工作?我试图用TEXT替换VARCHAR,但没有任何改变。

回答

1

首先解决方案。

查询中的问题是,您错过了转义值,正如其他人所说的那样。

每个字符串之前和之后所以添加'

mydatabase.execSQL("INSERT INTO SPENT (date,value,label1,label2,label3,label4,label5) VALUES('"+unixTime+"','"+str1+"','"+str2 
         +"','"+str3+"','"+str4+"','"+str5+"','"+str6+"');"); 

现在说这个,我不得不说,你正在使用的方法是不适合它的最佳方式,既为代码semplicity安全原因和

如果你需要插入DATAS,请使用经典SqliteDatabase Insert functionhere is the doc about it

ContentValues values = new ContentValues(); 
values.put("date", myDateString); 
values.put("value", myValueString); 
...//and so on 

db.insert(tableName, null, values); 

你就完成了。希望这可以帮助

-1

您需要添加引号的值,如:

mydatabase.execSQL("INSERT INTO SPENT (date,value,label1,label2,label3,label4,label5) VALUES('"+unixTime+"','"+str1+"','"+str2+"','"+str3+"','"+str4+"','"+str5+"','"+str6+"');"); 
0

试图逃跑用”你的字符串。

mydatabase.execSQL("INSERT INTO SPENT (date,value,label1,label2,label3,label4,label5) VALUES('"+unixTime+"','"+str1+"','"+str2 
         +"','"+str3+"','"+str4+"','"+str5+"','"+str6+"');");