我很新,无法弄清楚错误
我不熟悉SQlite
所以我不确定我是否将错误的数据类型传递给DBHelper类或我做错了什么。
如果我错过任何信息,请告诉我,先谢谢
这是我的 logcat
04-16 22:31:07.341 21052-21052/com.example.android.inputtest E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.inputtest, PID: 21052
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:5156)
at android.view.View$PerformClick.run(View.java:20755)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5835)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5156)
at android.view.View$PerformClick.run(View.java:20755)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5835)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: android.database.sqlite.SQLiteException: near "jordytip": syntax error (code 1): , while compiling: CREATE TABLE Tips(_id INTEGER PRIMARY KEY AUTOINCREMENT jordytip INTEGER mariatip INTEGER )
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1093)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:670)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:59)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1795)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1726)
at com.example.android.inputtest.MyDBHandler.onCreate(MyDBHandler.java:32)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
at com.example.android.inputtest.MyDBHandler.addJordyTip(MyDBHandler.java:44)
at com.example.android.inputtest.MainActivity.submit(MainActivity.java:50)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5156)
at android.view.View$PerformClick.run(View.java:20755)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5835)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
这是我的主要活动
package com.example.android.inputtest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
int jordyTips = 388;
int mariaTips = 388;
int more;
int less;
EditText tipInput;
TextView jordyOutput;
MyDBHandler dbHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tipInput = (EditText) findViewById(R.id.tipInput);
jordyOutput = (TextView) findViewById(R.id.jordyOutput);
dbHandler = new MyDBHandler(this,null, null,1);
}
public void submit (View view){
String value= tipInput.getText().toString();
int newTip =Integer.parseInt(value);
if (newTip % 2 == 0){
more = newTip/2;
less = newTip/2;
}else{
more = newTip/2 + 1;
less = newTip/2;
}
if (jordyTips <= mariaTips) {
jordyTips += more;
TipData tip = new TipData(more);
dbHandler.addJordyTip(tip);
printDatabase();
mariaTips += less;
}else{
jordyTips += less;
TipData tip = new TipData(less);
dbHandler.addJordyTip(tip);
printDatabase();
mariaTips += more;}
displayQuantity();
}
public void displayQuantity() {
TextView quantityTextView1 = jordyOutput;
TextView quantityTextView2 = (TextView)findViewById(R.id.mariaOutput);
quantityTextView1.setText((jordyTips-more)+ "+" + more + " " + String.valueOf(jordyTips));
quantityTextView2.setText(String.valueOf(mariaTips)+ "+" + less);
}
public void printDatabase(){
String dbString = dbHandler.databaseToString();
jordyOutput.setText(dbString);
tipInput.setText("");
}
}
这是我的 TipData类
package com.example.android.inputtest;
/**
* Created by jordy on 4/16/2017.
*/
public class TipData {
private int _id;
private int _jordytip;
public TipData(int jordytip) {
this._jordytip = jordytip;
}
public void set_id(int _id) {
this._id = _id;
}
public void set_jordytip(int _jordytip) {
this._jordytip = _jordytip;
}
public int get_id() {
return _id;
}
public int get_jordytip() {
return _jordytip;
}
}
这是我的 DBHandler类
package com.example.android.inputtest;
/**
* Created by jordy on 4/16/2017.
*/
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class MyDBHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "tips.db";
public static final String TABLE_NAME = "Tips";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_JORDYTIP = "jordytip";
public static final String COLUMN_MARIATIP = "mariatip";
public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String query = "CREATE TABLE " + TABLE_NAME + "(" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT " +
COLUMN_JORDYTIP + " INTEGER " +
COLUMN_MARIATIP + " INTEGER " +
")";
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP TABLE IF EXISTS" + TABLE_NAME);
onCreate(db);
}
public void addJordyTip(TipData jordytip){
ContentValues values = new ContentValues();
values.put(COLUMN_JORDYTIP,jordytip.get_jordytip() );
SQLiteDatabase db = getWritableDatabase();
db.insert(TABLE_NAME, null, values);
db.close();
}
public void deleteTip(String jordyTip){
SQLiteDatabase db = getWritableDatabase();
db.execSQL("DELETE FROM" + TABLE_NAME + " WHERE " + COLUMN_JORDYTIP + "=\"" + jordyTip + "\";");
}
public String databaseToString(){
String dbString= "";
SQLiteDatabase db = getWritableDatabase();
String query = "SELECT * FROM" + TABLE_NAME + "WHERE 1";
Cursor c = db.rawQuery(query, null);
c.moveToFirst();
while(!c.isAfterLast()){
if (c.getString(c.getColumnIndex("jordytip"))!=null){
dbString += c.getString(c.getColumnIndex("jordytip"));
dbString += "\n";
}
}
db.close();
return dbString;
}
}
立即获取此信息
04-17 00:17:58.181 20893-20893/com.example.android.inputtest E/SQLiteLog: (1) near "1": syntax error
04-17 00:17:58.191 20893-20893/com.example.android.inputtest E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.inputtest, PID: 20893
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:5156)
at android.view.View$PerformClick.run(View.java:20755)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5835)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5156)
at android.view.View$PerformClick.run(View.java:20755)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5835)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: android.database.sqlite.SQLiteException: near "1": syntax error (code 1): , while compiling: SELECT * FROM TipsWHERE 1
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1093)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:670)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:59)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1437)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1376)
at com.example.android.inputtest.MyDBHandler.databaseToString(MyDBHandler.java:63)
at com.example.android.inputtest.MainActivity.printDatabase(MainActivity.java:83)
at com.example.android.inputtest.MainActivity.submit(MainActivity.java:51)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5156)
at android.view.View$PerformClick.run(View.java:20755)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5835)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
然而,当我在
之前添加空格时String query = "SELECT * FROM " + TABLE_NAME + " WHERE 1";
我的应用没响应
答案 0 :(得分:3)
引起:android.database.sqlite.SQLiteException:near&#34; jordytip&#34;: 语法错误(代码1):,编译时:CREATE TABLE提示(_id INTEGER PRIMARY KEY AUTOINCREMENT jordytip INTEGER mariatip INTEGER)
首先纠正查询
String query = "CREATE TABLE " + TABLE_NAME + "("
+ COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL ,"
+ COLUMN_JORDYTIP + " INTEGER ,"
+ COLUMN_MARIATIP + " INTEGER" + ")";