[英]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.