我遇到了android和sqlite的問題。當我收到Arraylist中的數據時,應用程序崩潰。SQLite Arraylist崩潰android活動
的DbHelper代碼如下:
public ArrayList<String> getAllAccelerometerRegists() {
ArrayList<String> array_list = new ArrayList<String>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery("select * from acelerometer;", null);
res.moveToFirst();
while(res.isAfterLast() == false){
array_list.add(res.getString(1));
res.moveToNext();
}
return array_list;
}
活性代碼如下:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_data);
mydb = new DbHelper(this);
ArrayList array_list = mydb.getAllAccelerometerRegists();
}
的dbhelper類如下:
public class DbHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "sportsmove.db";
public static final String TABLE_ACCELEROMETER_NAME = "acelerometer";
public static final String TABLE_GYROSCOPE_NAME = "gyroscope";
public static final String TABLE_ORIENTATION_NAME = "orientation";
private static final int DATABASE_VERSION = 1;
public static final String ID = "_id";
public static final String X = "x";
public static final String ACCELEROMETERX = "x";
public static final String GYROSCOPEX = "x";
public static final String ORIENTATIONX = "x";
public static final String Y = "y";
public static final String ACCELEROMETERY = "y";
public static final String GYROSCOPEY = "y";
public static final String ORIENTATIONY = "y";
public static final String Z = "z";
public static final String ACCELEROMETERZ = "z";
public static final String GYROSCOPEZ = "z";
public static final String ORIENTATIONZ = "z";
public static final String W = "w";
public static final String ORIENTATIONW = "w";
public static final String TIMESTAMP = "timestamp";
private static final String DATABASE_CREATE = "create database " + DATABASE_NAME + ";";
private static final String DATABASE_CREATE_TABLE_ACCELEROMETER = "create table "
+ TABLE_ACCELEROMETER_NAME + "(" + ID
+ " integer primary key autoincrement, " + ACCELEROMETERX
+ " int, " + ACCELEROMETERY + " int, "
+ ACCELEROMETERZ+" int, " + TIMESTAMP + "int);";
private static final String DATABASE_CREATE_TABLE_GYROSCOPE = "create table "
+ TABLE_GYROSCOPE_NAME + "(" + ID
+ " integer primary key autoincrement, " + GYROSCOPEX
+ " int, " + GYROSCOPEY + " int, "
+ GYROSCOPEZ+" int, " + TIMESTAMP + "int);";
private static final String DATABASE_CREATE_TABLE_ORIENTATION = "create table "
+ TABLE_ORIENTATION_NAME + "(" + ID
+ " integer primary key autoincrement, " + ORIENTATIONX
+ " int, " + ORIENTATIONY + " int, "
+ ORIENTATIONZ +" int, "+ ORIENTATIONW +" int, " + TIMESTAMP + "int);";
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
db.execSQL(DATABASE_CREATE_TABLE_ACCELEROMETER);
db.execSQL(DATABASE_CREATE_TABLE_GYROSCOPE);
db.execSQL(DATABASE_CREATE_TABLE_ORIENTATION);
insertData(db);
}
public void insertData(SQLiteDatabase db) {
db.execSQL("insert into " + DATABASE_CREATE_TABLE_ACCELEROMETER + "values(0,0,0,0,0)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(DbHelper.class.getName(),
"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_ACCELEROMETER_NAME);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_GYROSCOPE_NAME);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_ORIENTATION_NAME);
onCreate(db);
}
public ArrayList<String> getAllAccelerometerRegists() {
ArrayList<String> array_list = new ArrayList<String>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery("select * from "+TABLE_ACCELEROMETER_NAME+";", null);
res.moveToFirst();
while(res.isAfterLast() == false){
array_list.add(res.getString(1));
res.moveToNext();
}
return array_list;
}
public boolean isTableExists(SQLiteDatabase db, String tableName) {
if (tableName == null || db == null || !db.isOpen())
{
return false;
}
Cursor cursor = db.rawQuery("SELECT COUNT(*) FROM "+TABLE_ACCELEROMETER_NAME+"WHERE", new String[] {"table", tableName});
if (!cursor.moveToFirst())
{
return false;
}
int count = cursor.getInt(0);
cursor.close();
return count > 0;
}
public boolean insertAccelerometerRegister (String x, String y, String z, String timestamp,String username, String moveName)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("x", x);
contentValues.put("y", y);
contentValues.put("z", z);
contentValues.put("timestamp", timestamp);
contentValues.put("username", username);
contentValues.put("moveName", moveName);
db.insert(TABLE_ACCELEROMETER_NAME, null, contentValues);
return true;
}
}
的logcat的誤差是如下:
FATAL EXCEPTION: main
Process: com.thalmic.android.sample.helloworld, PID: 11058
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.thalmic.android.sample.helloworld/com.thalmic.android.sample.helloworld.ViewData}: android.database.sqlite.SQLiteException: near "database": syntax error (code 1): , while compiling: create database sportsmove.db;
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2344)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2404)
at android.app.ActivityThread.access$800(ActivityThread.java:145)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1323)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5319)
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:1016)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)
Caused by: android.database.sqlite.SQLiteException: near "database": syntax error (code 1): , while compiling: create database sportsmove.db;
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:901)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:512)
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 database.DbHelper.onCreate(DbHelper.java:66)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:187)
at database.DbHelper.getAllAccelerometerRegists(DbHelper.java:93)
at com.thalmic.android.sample.helloworld.ViewData.onCreate(ViewData.java:26)
at android.app.Activity.performCreate(Activity.java:5976)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2297)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2404)
at android.app.ActivityThread.access$800(ActivityThread.java:145)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1323)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5319)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
顯示您的錯誤的logcat –
請從logcat添加一些日誌。它是找到/追蹤您的問題的最佳地點。 –
進程:com.thalmic.android.sample.helloworld,PID:11058 java.lang.RuntimeException:無法啓動活動ComponentInfo {com.thalmic.android.sample.helloworld/com.thalmic.android.sample.helloworld.ViewData }:android.database.sqlite.SQLiteException:near「database」:語法錯誤(代碼1):,編譯時:create database sportsmove.db; 引起:android.database.sqlite.SQLiteException:近「數據庫」:語法錯誤(代碼1):,編譯時:create database sportsmove.db; – codeOverflow