Android APP在getWritableDatabase()或getReadableDatabase()崩溃

时间:2014-01-29 03:42:06

标签: java android android-sqlite sqliteopenhelper

我是php开发人员,正在尝试学习android,我正在尝试设置数据库并插入一些数据。

我使用了一些教程,但是当我使用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


    }
}

我的模拟器在每次发布时都会崩溃,因此我无法使用手机进行测试。

我的min SDK是8,如果它是anyhelp。

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:2)

只需在中添加空格即

private static final String COLUMN_ID = "id";

我总是打印在日志代码中构造的db命令来检查,因为我总是用空格,括号或逗号犯错误。节省了很多心痛。

答案 1 :(得分:1)

正如@Larry建议的那样,您的查询存在问题。使用此

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)"; 

然后我认为你的代码会执行。但是,我认为您希望在Activity类中创建一些方法来从数据库中获取数据。这没有错,但更有效的方法如下。

DBHelper class中创建您的方法并在那里使用dbhelper.getReadableDatabase();。例如,如果您想获得表格中的总记录。将以下代码添加到DBHelper类。

public int getTotalRecords() {
        SQLiteDatabase sqLiteDatabase = this.getReadableDatabase();
        Cursor cursor = sqLiteDatabase.query(TABLE_LOCATIONS, null, null, null, null, null, null, null);
        return cursor.getCount();
    }

然后在MainActivity获得总数。像这样的记录

dbhelper = new DBHelper(this);
int count = dbhelper.getTotalRecords();