Android - SQLite多表创建

时间:2014-01-28 19:07:28

标签: android sqlite

这是我的示例数据库类。我只想在sqlite数据库上创建多个表。然后我在下面编写了这个数据库助手类。

package com.example.databasesample;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class MyDatabase extends SQLiteOpenHelper{

    public static final String TABLE_ROUTE = "route";   
    public static final String COL_RT_ID = "key";
    public static final String COL_RT_ROUTNM = "routnm";
    public static final String COL_RT_DES= "desc";
    public static final String COL_RT_LOGTME = "lgtime";
    private static final String DATABASE_CREATE_ROUTS = "create table "
            + TABLE_ROUTE + "( " 
            + COL_RT_ID + " integer primary key autoincrement, "
            + COL_RT_ROUTNM + " text not null, "
            + COL_RT_DES + " text, "
            + COL_RT_LOGTME + " text "
            + " );";

    /* TABLE USERMAS */
    public static final String TABLE_USERS = "usermas"; 
    public static final String COL_ID = "key";
    public static final String COL_USR = "user";
    public static final String COL_PASS = "pass";
    public static final String COL_LOGTME = "lgtime";
    /* TABLE USERMAS */
    private static final String DATABASE_CREATE_USERS= "create table "
            + TABLE_USERS + "( " 
            + COL_ID    + " integer primary key autoincrement, "
            + COL_USR   + " text not null, "
            + COL_PASS + " text not null, "
            + COL_LOGTME + " text "
            + " );";    


    /* TABLE ROUTS_DET */
    public static final String TABLE_CUSTOMER = "customers";    
    public static final String COL_CUST_ID = "key";
    public static final String COL_CUST_FOREGN ="key_f";
    public static final String COL_CUST_NM = "rname";   
    public static final String COL_CUST_TP= "tp";   
    public static final String COL_CUST_ADR= "des";
    public static final String COL_CUST_LOGTME = "lgtime";  
    /* TABLE ROUTS_DET */

    private static final String DATABASE_CREATE_CUSTMERS = "create table "
            + TABLE_CUSTOMER + "( " 
            + COL_CUST_ID   + " integer primary key autoincrement, "
            + COL_CUST_FOREGN + " integer not null, "
            + COL_CUST_NM   + " text, "
            + COL_CUST_TP + " text, "           
            + COL_CUST_ADR + " text, "          
            + COL_CUST_LOGTME + " text "
            + " );";

    public MyDatabase(Context context) {
        super(context, "mydb", null, 1);
        // TODO Auto-generated constructor stub
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(DATABASE_CREATE_ROUTS);
        db.execSQL(DATABASE_CREATE_CUSTMERS);
        db.execSQL(DATABASE_CREATE_USERS);
        Log.d("MYDATABASE", "onCreate");
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.d("MYDATABASE", "onUpgrade");
        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_CREATE_ROUTS);
        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_CREATE_ROUTS);
        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_CREATE_ROUTS);
        onCreate(db);

    }

}

然后我在onCreate方法下的MainActivity.java中实例化我的数据库类的对象。但是这段代码没有生成数据库和tables.where我错了吗?

package com.example.databasesample;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MyDatabase db = new MyDatabase(getApplicationContext());
        Log.d("MYACTIVIRY", "ACTIVITY CREATED");
        Log.d("MYACTIVIRY", db.getDatabaseName());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

1 个答案:

答案 0 :(得分:3)

第一次拨打onCreate()getReadableDatabase时会先致电{p> getWriteableDatabase

试试这段代码:

db = this.getWritableDatabase();

当你调用构造函数时,你只需要创建一个数据库,其中包含你在代码而不是表中提供的名称和版本,

让我回答任何问题