如何在android app

时间:2016-02-02 04:35:41

标签: android database sqlite

我在使用SQLite并在我的Android应用程序中创建数据库时遇到问题。我希望有人在这里可以指出我只是在创建数据库时做错了。这是代码:

在DBContract类中:

public final class DBContract {

public DBContract() { }    

public static abstract class DBEntry implements BaseColumns {

    public static final String TABLE_WORKOUTS = "Workouts";
    public static final String ENTRY_ID = "id";
    public static final String ENTRY_NAME = "Exercise";
    public static final String ENTRY_DESCRIPTION = "Description";
    public static final String ENTRY_SETS = "Sets";
    public static final String ENTRY_REPS = "Reps";
    public static final String ENTRY_WEIGHT = "Weight";


}

private static final String TEXT_TYPE = "TEXT";
private static final String COMMA_SEP = ",";
private static final String SQL_CREATE_ENTRIES =
        "CREATE TABLE " + DBEntry.TABLE_WORKOUTS + " (" +
                DBEntry.ENTRY_ID + " INTEGER PRIMARY KEY," +
                DBEntry.ENTRY_NAME + TEXT_TYPE + COMMA_SEP +
                DBEntry.ENTRY_DESCRIPTION + TEXT_TYPE + COMMA_SEP +
                DBEntry.ENTRY_SETS + TEXT_TYPE + COMMA_SEP +
                DBEntry.ENTRY_REPS + TEXT_TYPE + COMMA_SEP +
                DBEntry.ENTRY_WEIGHT + TEXT_TYPE  + " )";

private static final String SQL_DELETE_ENTRIES =
        "DROP TABLE IF EXISTS " + DBEntry.TABLE_WORKOUTS;


public static class DBHelper extends SQLiteOpenHelper {

    public static final int DATABASE_VERSION = 1;
    public static final String DATABASE_NAME = "DigiBottle.db";

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

    public void onCreate(SQLiteDatabase db) {
        db.execSQL(SQL_CREATE_ENTRIES);
    }

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // This database is only a cache for online data, so its upgrade policy is
        // to simply to discard the data and start over
        db.execSQL(SQL_DELETE_ENTRIES);
        onCreate(db);
    }

    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        onUpgrade(db, oldVersion, newVersion);
    }
  }
}

这就是我尝试访问数据库的方法:

public class MainActivity extends AppCompatActivity {

private BluetoothAdapter mBluetoothAdapter;
private BroadcastReceiver mReceiver;
private BluetoothSocket mmSocket;

private static int REQUEST_ENABLE_BT = 1;
private int hc05Index;
private String hc05Address;
private int numOfReadBytes;
private int readBuffer;
private ArrayList<String> deviceNameList = new ArrayList<String>();
private ArrayList<String> deviceAddressList = new ArrayList<String>();

DBContract.DBHelper mDBHelper = new DBContract.DBHelper(getBaseContext());

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    SQLiteDatabase db = mDBHelper.getWritableDatabase();

注意:我切断了主活动的onCreate()函数的其余部分,以便不添加必须滚动的不必要的代码。

因此在Main Activity中我创建了一个我的DBHelper类的实例,并在main活动的onCreate函数内部调用getWriteableDatabase()函数来获取现有数据库或创建一个新数据库(如果尚未创建) 。我的理解是这个函数将调用我的DBHelper类的onCreate()函数,它应该执行写入的Create Table SQL Command并创建数据库。由于某种原因,getWriteableDatabase()函数强制关闭应用程序。

非常感谢任何帮助!

提前致谢!!

编辑:这是日志:

02-01 22:04:48.681 21782-21782/com.example.rpholmes.digibottle E/AndroidRuntime: FATAL EXCEPTION: main
                                                                             Process: com.example.rpholmes.digibottle, PID: 21782
                                                                             java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.rpholmes.digibottle/com.example.rpholmes.digibottle.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase android.content.Context.openOrCreateDatabase(java.lang.String, int, android.database.sqlite.SQLiteDatabase$CursorFactory, android.database.DatabaseErrorHandler)' on a null object reference
                                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                                 at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                 at android.os.Looper.loop(Looper.java:148)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                              Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase android.content.Context.openOrCreateDatabase(java.lang.String, int, android.database.sqlite.SQLiteDatabase$CursorFactory, android.database.DatabaseErrorHandler)' on a null object reference
                                                                                 at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:223)
                                                                                 at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
                                                                                 at com.example.rpholmes.digibottle.MainActivity.onCreate(MainActivity.java:45)
                                                                                 at android.app.Activity.performCreate(Activity.java:6237)
                                                                                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                                 at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                 at android.os.Looper.loop(Looper.java:148) 
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                                 at java.lang.reflect.Method.invoke(Native Method) 
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

02-01 22:04:48.705 21782-21782 /? I /处理:发送信号。 PID:21782 SIG:9

2 个答案:

答案 0 :(得分:0)

SQL查询需要适当的空格。 将DBContract中的以下行修改为...

private static final String TEXT_TYPE = " TEXT ";
private static final String COMMA_SEP = ", ";

(如果错误仍然存​​在,请发布您的日志语句)

答案 1 :(得分:0)

您试图在创建表之前插入值,尝试添加SQLiteDatabase db = mDBHelper.getWritableDatabase();仅在DBHelper类中。 这样在创建表插入后就可以完成了。

 public void InsertValues(){
        db =this.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put(DBEntry.ENTRY_NAME,"adc");
    }