gpt4 book ai didi

android - SQLite——在android同版本下创建多个表

转载 作者:行者123 更新时间:2023-11-30 04:31:07 24 4
gpt4 key购买 nike

有一个应用程序需要在数据库中创建多个表。我用数据库编写了一个简单的类作业

package db.example.app;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Environment;


public class DB {

private final Map<String, String> FIELDS;

private DatabaseHelper dbHelper;
private SQLiteDatabase Db;

private final String DB_NAME = "tasstelecom";
private final String TABLE_NAME;

private final int DATABASE_VERSION;
private final String CREATE_TABLE;

private final Context context;

private class DatabaseHelper extends SQLiteOpenHelper {

DatabaseHelper(Context context) {
super(context, DB_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int OldVerison, int newVersion) {

db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}

public DB(Context ctx, String tableName, Map<String, String> fields, int version) {
this.context = ctx;

this.TABLE_NAME = tableName;
this.FIELDS = fields;
this.DATABASE_VERSION = version;

String createFields = "";

for(Map.Entry<String, String> m: FIELDS.entrySet()) {
createFields += m.getKey().toString()+" "+m.getValue().toString()+", ";
}

createFields = createFields.replaceAll(",\\s$", "");

this.CREATE_TABLE = "create table "+tableName+" (" + createFields+")";
}

public DB open() throws SQLException {
dbHelper = new DatabaseHelper(context);
Db = dbHelper.getWritableDatabase();

return this;
}

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

public long insert(Map<String,String> vals) {
ContentValues initialValues = new ContentValues();

for(Map.Entry<String, String> m : vals.entrySet()) {
initialValues.put(m.getKey(), m.getValue());
}

return Db.insert(TABLE_NAME, null, initialValues);
}

public Cursor executeQuery(String[] fieldsArray) {
return Db.query(TABLE_NAME, fieldsArray, null, null, null, null, null);
}

public void backupDatabase() throws IOException {
String inFileName = "/data/data/ru.ashot.tasstelecom/databases/"+DB_NAME;
File dbFile = new File(inFileName);
FileInputStream fis = new FileInputStream(dbFile);

String outFileName = Environment.getExternalStorageDirectory()+"/"+DB_NAME+".sqlite";

OutputStream output = new FileOutputStream(outFileName);

byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer))>0){
output.write(buffer, 0, length);
}

output.flush();
output.close();
fis.close();
} }

调用构造函数创建数据库,如果不存在则创建表

table1 = new DB(this, "table1", fields1, 1);
table2 = new DB(this, "table2", fields1, 2);

它工作正常,但每次我调用构造函数时,我都需要指定新的数据库版本号,这有点不舒服。

有没有一种方法可以用我在相同版本的数据库下描述的方法创建多个表?

最佳答案

不需要多次调用constructor,一次就够了。替换:

private final String CREATE_TABLE;

与:

protected ArrayList<String> mSqlQueries = new ArrayList<String>();

然后改变这个:

this.CREATE_TABLE = "create table "+tableName+" (" + createFields+")";

收件人:

String sql = "create table "+tableName+" (" + createFields+")";
mSqlQueries.add(sql);

并且改变onCreate如下:

@Override
public void onCreate(SQLiteDatabase db) {
for(String sql : mSqlQueries) {
db.execSQL(sql);
}
}

关于android - SQLite——在android同版本下创建多个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7693179/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com