Android SQLite数据库无法创建

时间:2014-06-18 06:54:32

标签: android database sqlite

我是Android的新手,想为我的Android应用程序设计数据库,但我无法做到这一点。我已经做了一切来找到错误,但无法找到任何错误,仍然无法创建数据库。请有人帮助我,提前谢谢。

代码如下:

模特课程:

package com.example.health;

public class Daily {
private int _id;
private String _tips;

public Daily(){
}

public Daily(int id, String tips){
    this._id=id;
    this._tips=tips;
}
public Daily(String tips){
    this._tips=tips;
}
public void setID(int id) {
    this._id = id;
}
public void setTips(String tips){
    this._tips=tips;
}
public int getID() {
    return this._id;
}
public String getTips(){
    return this._tips;
}
  }

数据库处理程序:

 package com.example.health;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;


public class MyDBHandler extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "healthDB.db";
private static final String TABLE_DAILY = "daily";

public static final String COLUMN_ID = "_id";
public static final String COLUMN_TIPS = "tips";

public MyDBHandler(Context context, String name, 
        CursorFactory factory, int version) {
    super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_DAILY_TABLE = "CREATE TABLE " + TABLE_DAILY + "(" + COLUMN_ID + " INTEGER PRIMARY KEY," + COLUMN_TIPS + " TEXT " + ")";
      db.execSQL(CREATE_DAILY_TABLE);

      }

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_DAILY);
      onCreate(db);
}
public void addDaily(Daily daily) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(COLUMN_TIPS, daily.getTips());
    db.insert(TABLE_DAILY, null, values);
    db.close();
}
 public List<Daily> getAllDailys(){
        List <Daily> dailyList=new ArrayList<Daily>();

        // Select All Query
        String selectQuery="SELECT * FROM " + TABLE_DAILY;

        SQLiteDatabase db=this.getReadableDatabase();
        Cursor c=db.rawQuery(selectQuery, null);

        // looping through all rows and adding to the list
        if(c.moveToFirst()){
            do{
                Daily daily=new Daily();
                daily.setID(Integer.parseInt(c.getString(0)));
                daily.setTips(c.getString(1));


                // Adding user to the list
                dailyList.add(daily);
            }while(c.moveToNext());
        }
        c.close();
        return dailyList;
}}

2 个答案:

答案 0 :(得分:0)

请注意,只有在对数据库执行某些操作后,才会创建数据库。即只需创建SQLiteOpenHelper类, NOT 就会在设备中创建数据库,直到您对数据库执行某些读/写操作

[编辑1] ,直到您执行以下操作:

MyDBHandler db = new MyDBHandler(....);
db.addDaily(daily);

将不会创建数据库。

[编辑2] 您可能还想更改MyDBHandler类'构造函数:

public MyDBHandler(Context contex) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

[注意] 我假设您没有收到任何错误/例外。

答案 1 :(得分:0)

您的DbClass

package com.example.demo;
import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;


public class MyDBHandler extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "healthDB.db";
private static final String TABLE_DAILY = "daily";

public static final String COLUMN_ID = "_id";
public static final String COLUMN_TIPS = "tips";

static MyDBHandler mDbHelper;

private MyDBHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

public static synchronized MyDBHandler getInstance(Context context) {
    if (mDbHelper == null) {
        mDbHelper = new MyDBHandler(context);
    }
    return mDbHelper;
}

@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_DAILY_TABLE = "CREATE TABLE " + TABLE_DAILY + "(" + COLUMN_ID + " INTEGER PRIMARY KEY," + COLUMN_TIPS + " TEXT " + ")";
      db.execSQL(CREATE_DAILY_TABLE);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_DAILY);
      onCreate(db);
}
public void addDaily(Daily daily) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(COLUMN_TIPS, daily.getTips());
    db.insert(TABLE_DAILY, null, values);
    db.close();
}
 public List<Daily> getAllDailys(){
        List <Daily> dailyList=new ArrayList<Daily>();

        // Select All Query
        String selectQuery="SELECT * FROM " + TABLE_DAILY;

        SQLiteDatabase db=this.getReadableDatabase();
        Cursor c=db.rawQuery(selectQuery, null);

        // looping through all rows and adding to the list
        if(c.moveToFirst()){
            do{
                Daily daily=new Daily();
                daily.setID(Integer.parseInt(c.getString(0)));
                daily.setTips(c.getString(1));


                // Adding user to the list
                dailyList.add(daily);
            }while(c.moveToNext());
        }
        c.close();
        return dailyList;
}}

您可以添加此类数据

MyDBHandler db = MyDBHandler.getInstance(getApplicationContext());
Daily daily = new Daily();
daily.setID(1);
daily.setTips("hello");
db.addDaily(daily);

我测试了它。它工作正常..