我使用Android Studio和SQLite构建App。如何将多个表组成一个数据库?
我为我的DataHelper编写了这段代码并且不确定它是对的。
echo "<script>
$(window).load(function(){
$('#mymodal').modal('show');
});
</script>";
}
答案 0 :(得分:1)
您的代码运行正常。我创建了简单的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 /? I /来自Table1 :: 1 Jana 1234的数据
09-24 08:39:30.576 9607-9607 /? I /来自Table2 :: 1 Mudita 1234的数据
答案 1 :(得分:0)
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);
.
.
执行多个Create Table Queries可以在一个数据库中创建多个表。
答案 2 :(得分:0)
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);
}
}