admin管理员组文章数量:1395785
I used Android Studio and SQLite to build App. How do I make multiple tables into one database?
I had wrote this code for my DataHelper and unsure it's right.
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
}
}
I used Android Studio and SQLite to build App. How do I make multiple tables into one database?
I had wrote this code for my DataHelper and unsure it's right.
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
}
}
Share Improve this question edited Sep 24, 2017 at 3:35 FluffyKitten 14.3k10 gold badges43 silver badges54 bronze badges asked Sep 24, 2017 at 3:24 agungyunaedyagungyunaedy 211 gold badge1 silver badge3 bronze badges 1- This seems reasonable ... if you run different create table statements in the same database, you get several tables in one database. – okaram Commented Sep 24, 2017 at 3:26
3 Answers
Reset to default 2your code works fine. I created simple MainActivity to illustrate it - wele to try it out.
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);
}
}
}
Output prints:
09-24 08:39:30.575 9607-9607/? I/Data from Table1:: 1 Jana 1234
09-24 08:39:30.576 9607-9607/? I/Data from Table2:: 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);
.
.
Execution of multiple Create Table Queries can create multiple tables in one database.
DatabaseHelper.java
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);
}
}
本文标签: javascriptmake multiple tables into one database SQLiteStack Overflow
版权声明:本文标题:javascript - make multiple tables into one database SQLite - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744759487a2623666.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论