【发布时间】:2015-07-21 11:08:17
【问题描述】:
更新
我找到了这个链接
http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/
谁能解释我将如何使用这个来编写更新/插入语句
___________________________________________________________________________
问题
我正在尝试创建一种方法,我可以将存储在 sqllite 数据库中的数据复制到我的应用程序的数据库中。结构类似。我只是不想一一插入行。
我正在寻找一种方法,通过该方法我可以将存储在资产文件夹或外部存储中的预先输入的数据复制到我的应用程序数据库中。
我在我的应用程序中填写数据时遇到问题。我不想以编程方式为每一行和每一列编写 5000 个插入语句。
谁能推荐一个方法。我是android新手,所以请详细解释或提供链接。
我的数据库助手类
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBHelper extends SQLiteOpenHelper {
public SQLiteDatabase db1;
// Static Final Variable database meta information
static final String DATABASE = "assesmenttool.db";
static final int DATABASE_VERSION = 1;
//Table Student Details
static final String TABLEStudent = "StudentDetails";
static final String S_ID = "_id";
static final String SchoolID = "SchoolID";
static final String SchoolName = "schoolname";
static final String StudentFirstName = "StudentFirstName";
static final String StudentLastName ="StudentLastName";
static final String StudentClassLevel ="StudentClassLevel";
static final String RollNo="RollNo";
static final String TestDate ="TestDate";
//Table Response Details
static final String TABLEResponse = "TableResponse";
static final String R_ID = "_id";
static final String StudentID = "StudentID";
static final String R_QuestionID = "QuestionID";
static final String QuestOptionID = "QuestOptionID";
//Table Question Master
static final String TableQuestionMaster = "TableQuestionMaster";
static final String Q_ID= "_id";
static final String Module_ID = "Module_ID";
static final String SubModule_ID = "SubModule_ID";
static final String SubModuleQuestion_ID ="SubModuleQuestion_ID";
static final String Question_ID= "Question_ID";
static final String Title = "Title";
static final String Module = "Module";
static final String TitleDescription = "TitleDescription";
static final String QuestionText = "QuestionText";
static final String QuestionImage = "QuestionImage";
static final String QuestionTemplate = "QuestionTemplate";
static final String CorrectOptionID = "CorrectOptionID";
//Table Template Master
static final String TableTemplateMaster = "TemplateMaster";
static final String T_ID= "_id";
static final String Template_ID= "Template_ID";
static final String Name = "Name";
static final String Description = "Description";
//Table Question Option
static final String TableQuestionOption = "TableQuestionOption";
static final String TQP_ID= "_id";
static final String TQP_QuestionID = "QuestionID";
static final String OptionText = "OptionText";
//Table Class Master
static final String TableClassMaster = "TableClassMaster";
static final String Class_ID= "_id";
static final String Class = "class";
// Override constructor
public DBHelper(Context context) {
super(context, DATABASE, null, DATABASE_VERSION);
}
// Override onCreate method
@Override
public void onCreate(SQLiteDatabase db) {
//Create Table Student Details
db.execSQL("CREATE TABLE " + TABLEStudent + " ( " + S_ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + SchoolID + " text, "
+ SchoolName + " text, " + StudentFirstName + " text, " + StudentLastName + " text, " + RollNo + " text," + TestDate + " text," + StudentClassLevel + " text)");
//Create Table Response Details
db.execSQL("CREATE TABLE " + TABLEResponse + " ( " + R_ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + StudentID + " text, "
+ R_QuestionID + " text, " + QuestOptionID + " text)");
//Create Table Question Master
db.execSQL("CREATE TABLE " + TableQuestionMaster + " ( " + Q_ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + Question_ID + " text, " + Module_ID + " text, " + SubModule_ID + " text, " + SubModuleQuestion_ID + " text, " + Title + " text, " + Module + " text,"
+ TitleDescription + " text, " + QuestionText + " text, " + QuestionImage + " text, " + QuestionTemplate + " text," + CorrectOptionID + " text)");
//Create Table Template Master
db.execSQL("CREATE TABLE " + TableTemplateMaster + " ( " + T_ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + Name + " text, "
+ Description + " text)");
//Create Table Question Option
db.execSQL("CREATE TABLE " + TableQuestionOption + " ( " + TQP_ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + TQP_QuestionID + " text, "
+ OptionText + " text)");
//Create Table Class Master
db.execSQL("CREATE TABLE " + TableClassMaster + " ( " + Class_ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + Class + " text)");
}
public List<String> getAllClasses(){
List<String> labels = new ArrayList<String>();
// Select All Query
String selectQuery = "SELECT * FROM " + TableClassMaster;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
labels.add(cursor.getString(1));
} while (cursor.moveToNext());
}
// closing connection
cursor.close();
db.close();
// returning lables
return labels;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop old version table
db.execSQL("Drop table " + TABLEStudent);
db.execSQL("Drop table " + TABLEResponse);
db.execSQL("Drop table " + TableQuestionMaster);
db.execSQL("Drop table " + TableTemplateMaster);
db.execSQL("Drop table " + TableQuestionOption);
db.execSQL("Drop table " + TableClassMaster);
// Create New Version table
onCreate(db);
}
}
我在 stackoverflow 上找到的 SQLLITE 复制类
如何将这两者混合在一起。我如何同步这两个 sqllite 表。 ?
private static final String DB_NAME = "asset.db";
private Context context;
public AssetDatabaseOpenHelper(Context context) {
this.context = context;
}
public SQLiteDatabase openDatabase() {
File dbFile = context.getDatabasePath(DB_NAME);
if (!dbFile.exists()) {
try {
copyDatabase(dbFile);
} catch (IOException e) {
throw new RuntimeException("Error creating source database", e);
}
}
return SQLiteDatabase.openDatabase(dbFile.getPath(), null, SQLiteDatabase.OPEN_READONLY);
}
private void copyDatabase(File dbFile) throws IOException {
InputStream is = context.getAssets().open(DB_NAME);
OutputStream os = new FileOutputStream(dbFile);
byte[] buffer = new byte[1024];
while (is.read(buffer) > 0) {
os.write(buffer);
}
os.flush();
os.close();
is.close();
}
}
【问题讨论】:
-
Use
SQLiteAssetHelper用于将数据库打包为应用中的资产。 -
好的,让我检查一下
-
我只想将我的数据库中的表中的值从资产文件夹复制到应用程序的数据库
-
我不打算创建一个新表。我只是想要一个简单的复制机制