Android sqlite 打开数据库崩溃

标签 android

我有两个类,一个是主类,另一个是处理 sqlite 的类。我遇到的问题是当它到达主类中的 db.open() 时它崩溃了,我不知道为什么。

主类:

public class RingerSchedule extends Activity 
{ 
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);        
        DBAdapter db = new DBAdapter(this);
        db.open(); //crash!
        //more stuff
    }
}

sqlite 类:

public class DBAdapter
{
    public static final String KEY_ROWID = "id";
    public static final String KEY_TO = "to";
    public static final String KEY_FROM = "from";
    public static final String KEY_DAY = "day";    
    public static final String KEY_FUNCTION = "function";    
    private static final String TAG = "DBAdapter";

    private static final String DATABASE_NAME = "myDB";
    private static final String DATABASE_TABLE = "schedules";
    private static final int DATABASE_VERSION = 1;

    private final Context context; 

    private DatabaseHelper DBHelper;
    private SQLiteDatabase db;

    public DBAdapter(Context ctx) 
    {
        this.context = ctx;
        DBHelper = new DatabaseHelper(context); 
    }

    private static class DatabaseHelper extends SQLiteOpenHelper 
    {
        DatabaseHelper(Context context) 
        {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) 
        {
            db.execSQL("CREATE TABLE "
                + DATABASE_TABLE
                + " (id INT PRIMARY KEY AUTOINCREMENT, to TEXT,"
                + " from TEXT, day TEXT"
                + " function TEXT);"); 
        }
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
        {
            Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS titles");
            onCreate(db);
        }
    }    

    //opens the database
    public DBAdapter open() throws SQLException 
    {
        this.db = DBHelper.getWritableDatabase();
        return this;
    }

    //closes the database
    public void close() 
    {
        DBHelper.close();
    }

    //insert a schedule into the database
    public long insertSchedule(String from, String to, String day, String function) 
    {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_TO, to);
        initialValues.put(KEY_FROM, from);
        initialValues.put(KEY_DAY, day);
        initialValues.put(KEY_FUNCTION, function);
        return db.insert(DATABASE_TABLE, null, initialValues);
    }

    //deletes a particular schedule
    public boolean deleteSchedule(long Id) 
    {
        return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + Id, null) > 0;
    }

    //retrieves all the schedules
    public Cursor getAllSchedules() 
    {
        return db.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TO, KEY_FROM, KEY_DAY, KEY_FUNCTION}, null, null, null, null, null, null);
    }

    //updates a schedule
    public boolean updateSchedule(long Id, String to, String from, String day, String function) 
    {
        ContentValues args = new ContentValues();
        args.put(KEY_TO, to);
        args.put(KEY_FROM, from);
        args.put(KEY_DAY, day);
        args.put(KEY_FUNCTION, function);
        return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + Id, null) > 0;
    }
}

最佳答案

您是否在主数据库的某处关闭了数据库?如果没有,请尝试关闭它。 :)

关于Android sqlite 打开数据库崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3691959/

相关文章:

android 新 Activity ,每个屏幕的新布局还是有更好/更清洁的方式?

机器人 : AES encryption/decryption using openSSL

java - RadioButton 在布局之间切换

java - 单选按钮 : Listener in XML or JAVA?

android - 找不到 adb.exe

android - 我应该使用cocos2D还是andengine?

java - 如何在Android中显示特殊字符?

Android Inject Dependency (Dagger 2) Inside Fragment 使用@ContributesAndroidInjector

android - 将变量的值从第一个 Activity 传递到第三个 Activity

android - 后退按钮关闭应用程序而不是转到上一个 fragment android 导航组件