我在代码中没有任何错误,但是当我运行应用程序时,它会崩溃。这是日志的图片。 http://postimg.org/image/bbcovjovn/我无法弄清楚代码中的错误。
MyBowlingScoresApplication.java
import java.util.ArrayList;
import android.app.Application;
import android.database.sqlite.SQLiteDatabase;
public class MyBowlingScoresApplication extends Application {
private ArrayList<BowlingScores> allBowlingScores;
private SQLiteDatabase bowlingScoresDB;
@Override
public void onCreate() {
super.onCreate();
BowlingScoresDatabaseHelper databaseHelper1 = new BowlingScoresDatabaseHelper(this);
bowlingScoresDB = databaseHelper1.getWritableDatabase();
//TODO get the data out of the database
allBowlingScores = new ArrayList<BowlingScores>();
}
public void addBowlingScores(BowlingScores bowlingScores){
assert bowlingScores != null;
allBowlingScores.add(bowlingScores);
}
public ArrayList<BowlingScores> getAllBowlingScores() {
return allBowlingScores;
}
private void setAllBowlingScores(ArrayList<BowlingScores> allBowlingScores) {
this.allBowlingScores = allBowlingScores;
}
}
BowlingScoresDatabaseHelper.java
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class BowlingScoresDatabaseHelper extends SQLiteOpenHelper {
public static final int DB_VERSION = 1;
public static final String DB_NAME = "MyBowlingScores.SQLite";
public static String BOWLING_SCORES_TABLE="BowlingScoresTable";
public static String RECORD_ID="ID";
public static String DATE = "Date";
public static String GAME1 = "Game1";
public static String GAME2 = "Game2";
public static String GAME3 = "Game3";
public BowlingScoresDatabaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase bowlingScoresDB) {
String sqlStatement = "create table " + BOWLING_SCORES_TABLE
+ " ("
+ RECORD_ID + " integer primary key autoincrement not null,"
+ DATE + " integer,"
+ GAME1 + " integer,"
+ GAME2 + " integer,"
+ GAME3 + " integer,"
+ ");";
Log.d("Bowling Database", sqlStatement);
bowlingScoresDB.execSQL(sqlStatement);
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:4)
SQLite查询的语法错误:
create tableBowlingScoresTable...
应该是这样的:
create table BowlingScoresTable...
忘了空间?
编辑:
好的,你现在提供了代码。我能看到的另一个语法错误是:
+ GAME3 + " integer,"
由于跟随)
,最后不应该有逗号。
答案 1 :(得分:1)
我建议getting familiar with Android's wide set of debugging tools。通过检查Logcat中的堆栈跟踪,或者在连接调试器时发生崩溃时查看调用层次结构,您可以轻松查明问题的原因。
堆栈跟踪清楚地说明了用于创建表的SQL语句包含无效语法。我发现了两个问题: