繁体   English   中英

将多个表合并为一个数据库 SQLite

[英]make multiple tables into one database SQLite

我使用 Android Studio 和 SQLite 来构建应用程序。 如何将多个表合并到一个数据库中?

我为我的 DataHelper 编写了这段代码,但不确定它是否正确。

public class DataHelper extends SQLiteOpenHelper {

private static final String DATABASE_NAME = "pdkb.db";
private static final int DATABASE_VERSION = 1;
public DataHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    // TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    String sql = "create table admin(id integer primary key, nama text null, pasword text null);";
    String sql2 = "create table user(id integer primary key, nama text null, pasword text null);";
    Log.d("Data", "onCreate: " + sql);
    Log.d("Data", "onCreate: " + sql2);
    db.execSQL(sql);
    db.execSQL(sql2);
    sql = "INSERT INTO admin (id, nama, pasword) VALUES ('01', 'Jana', '1234'); ";
    sql2= "INSERT INTO user (id, nama, pasword) VALUES ('01', 'Mudita', '1234');";
    db.execSQL(sql);
    db.execSQL(sql2);
}

@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
    // TODO Auto-generated method stub

}

}

你的代码工作正常。 我创建了简单的 MainActivity 来说明它 - 欢迎尝试。

package <your package>;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends AppCompatActivity {

    private DataHelper databaseOpenHelper = null; // database helper
    private SQLiteDatabase database = null; // database object


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        databaseOpenHelper = new DataHelper(this);

        database = databaseOpenHelper.getWritableDatabase();


        Cursor cursor1 = database.query(
                "admin", //is the table
                null, //null for all colm
                null,//where
                null,//where argument for where placeholder's
                null,//group by
                null,//having
                null //ordered by
        );

        Cursor cursor2 = database.query(
                "user", //is the table
                null, //null for all colm
                null,//where
                null,//where argument for where placeholder's
                null,//group by
                null,//having
                null //ordered by
        );


        Integer iID = -1;
        String sID = "-1";
        String nAme = "kkk";
        String pSswd = "lll";

        while (cursor1.moveToNext()) {
            iID = cursor1.getInt(0);
            sID = iID.toString();
            nAme = cursor1.getString(1);
            pSswd = cursor1.getString(2);
            Log.i("Data from Table1:", sID + " " + nAme + " " + pSswd);
        }

        while (cursor2.moveToNext()) {
            iID = cursor2.getInt(0);
            sID = iID.toString();
            nAme = cursor2.getString(1);
            pSswd = cursor2.getString(2);
            Log.i("Data from Table2:", sID + " " + nAme + " " + pSswd);
        }
    }
}

输出打印:

09-24 08:39:30.575 9607-9607/? 表 1 中的 I/数据:: 1 Jana 1234

09-24 08:39:30.576 9607-9607/? I/来自表 2 的数据:: 1 Mudita 1234

String sql = "create table [table1_name](id integer primary key,...);";
String sql2 = "create table [table2_name](id integer primary key,..);";
.
.
.
db.execSQL(sql);
db.execSQL(sql2);
.
.

执行多个创建表查询可以在一个数据库中创建多个表。

数据库助手

public class DataHelper extends SQLiteOpenHelper {

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

static final String CREATE_TABLE_ADMIN = "create table " + "admin" + "( "
        + "ID" + "  integer primary key autoincrement," + "NAME text,"
        + "PASSWORD text); ";

static final String CREATE_TABLE_USER = "create table " + "user" + "( "
        + "ID" + "  integer primary key autoincrement," + "NAME text,"
        + "PASSWORD text); ";

public static SQLiteDatabase db;

public LoginDataBaseAdapter(Context context) {
    super(context, DATABASE_NAME, null,1);
}
@Override
public void onCreate(SQLiteDatabase _db) {
    _db.execSQL(CREATE_TABLE_ADMIN);
    _db.execSQL(CREATE_TABLE_USER);
}

@Override
public void onUpgrade(SQLiteDatabase _db, int _oldVersion, int _newVersion)       {
    onCreate(_db);
}


public LoginDataBaseAdapter open() throws SQLException {
    db = this.getWritableDatabase();
    return this;
}

public void close() {
    db.close();
}

public void insertEntry_Admin(int id,String Name, String password) {

    ContentValues newValues = new ContentValues();
    newValues.put("ID",id);
    newValues.put("NAME", Name);
    newValues.put("PASSWORD", password);
    db.insert("admin", null, newValues);
}

public void insertEntry_User(int id,String Name, String password) {

    ContentValues newValues = new ContentValues();
    newValues.put("ID",id);
    newValues.put("NAME", Name);
    newValues.put("PASSWORD", password);
    db.insert("user", null, newValues);
}
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM