崩潰我是php開發人員,試圖學習android,我試圖設置一個數據庫並插入一些數據。Android應用程序在getWritableDatabase()或getReadableDatabase()
我跟着一些教程,但我的應用程序卡住了,當我使用getWritableDatabase()
或getReadableDatabase()
。
以下是我創建的我的DBHelper
的代碼。
package com.example.bootstart;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBHelper extends SQLiteOpenHelper {
private static final String LOGTAG = "THEDBHELPER";
private static final String DATABASE_NAME = "geolocation.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_LOCATIONS = "locations";
private static final String COLUMN_ID = "id";
private static final String COLUMN_welawa = "welawa";
private static final String COLUMN_lati = "latitude";
private static final String COLUMN_longi = "longitude";
private static final String TABLE_CREATE = "CREATE TABLE " + TABLE_LOCATIONS + " ("
+ COLUMN_ID + "INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_welawa + " TEXT, " + COLUMN_lati
+ " TEXT, " + COLUMN_longi + " TEXT)";
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(TABLE_CREATE);
Log.i(LOGTAG, "TABLE HAS BEEN CREATED");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_LOCATIONS);
onCreate(db);
}
}
我通過我的MainActivity類訪問它。
package com.example.bootstart;
import android.os.Bundle;
import android.widget.Toast;
import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class MainActivity extends Activity {
SQLiteOpenHelper dbhelper;
SQLiteDatabase database;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(getApplicationContext(), "Main Activity", Toast.LENGTH_LONG).show();
dbhelper = new DBHelper(this);
//Works upto here
database = dbhelper.getReadableDatabase(); //App crashes on this line
}
}
我的模擬器在每次啓動時崩潰,所以我一直在用我的手機進行測試。
我最小的SDK是8,如果它是任何幫助。
任何幫助是非常感謝的傢伙。
我不知道這是你的崩潰的原因,但你的CREATE語句無效。 'id'後面沒有空格,所以你的字符串將是..「.... idINTEGER PRIMARY ....」。首先解決這個問題,然後重試。 – Larry
張貼您的logcat –
@Larry,傻我。有效。謝謝larry。把它作爲答案將雅? :P –