我想創建一個android測驗應用程序。它包含大約200個問題。我想知道如何插入問題。通過java代碼(我將創建數據庫並插入問題)或直接導入數據庫(我已經創建)。使用sqlitedatabase測驗應用程序
-3
A
回答
0
如果你已經數據庫文件,你可以使用這個例子
public class Databasehelper extends SQLiteOpenHelper
{
private SQLiteDatabase myDataBase;
private final Context myContext;
private static final String DATABASE_NAME = "db.sqlite";
public final static String DATABASE_PATH ="/data/data/com.shir60bhushan/databases/";
public static final int DATABASE_VERSION = 1;
//public static final int DATABASE_VERSION_old = 1;
//Constructor
public Databasehelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.myContext = context;
}
//Create a empty database on the system
public void createDatabase() throws IOException
{
boolean dbExist = checkDataBase();
if(dbExist)
{
Log.v("DB Exists", "db exists");
// By calling this method here onUpgrade will be called on a
// writeable database, but only if the version number has been
// bumped
//onUpgrade(myDataBase, DATABASE_VERSION_old, DATABASE_VERSION);
}
boolean dbExist1 = checkDataBase();
if(!dbExist1)
{
this.getReadableDatabase();
try
{
this.close();
copyDataBase();
}
catch (IOException e)
{
throw new Error("Error copying database");
}
}
}
//Check database already exist or not
private boolean checkDataBase()
{
boolean checkDB = false;
try
{
String myPath = DATABASE_PATH + DATABASE_NAME;
File dbfile = new File(myPath);
checkDB = dbfile.exists();
}
catch(SQLiteException e)
{
}
return checkDB;
}
//Copies your database from your local assets-folder to the just created empty database in the system folder
private void copyDataBase() throws IOException
{
String outFileName = DATABASE_PATH + DATABASE_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
InputStream myInput = myContext.getAssets().open(DATABASE_NAME);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0)
{
myOutput.write(buffer, 0, length);
}
myInput.close();
myOutput.flush();
myOutput.close();
}
//delete database
public void db_delete()
{
File file = new File(DATABASE_PATH + DATABASE_NAME);
if(file.exists())
{
file.delete();
System.out.println("delete database file.");
}
}
//Open database
public void openDatabase() throws SQLException
{
String myPath = DATABASE_PATH + DATABASE_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}
public synchronized void closeDataBase()throws SQLException
{
if(myDataBase != null)
myDataBase.close();
super.close();
}
public void onCreate(SQLiteDatabase db)
{
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
if (newVersion > oldVersion)
{
Log.v("Database Upgrade", "Database version higher than old.");
db_delete();
}
}
//add your public methods for insert, get, delete and update data in database.
}
+1
你的回答有意義嗎? –
0
訪問您可以使用下面的庫
編譯 'com.readystatesoftware.sqliteasset:sqliteassethelper:+'
並把已創建的數據庫放在
個資產/數據庫/ northwind.db
這個目錄並做
public class MyDatabase extends SQLiteAssetHelper {
private static final String DATABASE_NAME = "northwind.db";
private static final int DATABASE_VERSION = 1;
public MyDatabase(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
}
+0
獲取更多信息https://github.com/jgilfelt/android-sqlite-asset-helper –
相關問題
- 1. 使用滑軌測驗應用程序
- 2. AngularJS測驗應用程序
- 3. 測驗應用程序
- 4. Android測驗應用程序
- 5. 使用陣列隨機測驗應用程序使用數組
- 6. Android測驗應用程序錯誤
- 7. Android測驗應用程序設計。 ViewFlipper?
- 8. 如何創建測驗應用程序?
- 9. 測驗應用程序 - 循環問題
- 10. 測驗遊戲應用程序
- 11. 的Android應用程序測驗崩潰
- 12. 測驗應用程序+單身類as3
- 13. 編寫測驗應用程序
- 14. 驗收測試Java OpenGL應用程序
- 15. 倒計時器測驗應用程序
- 16. Vb.Net測驗應用程序多視圖
- 17. 創建測驗應用程序Asp.net
- 18. 使用ASP.NET MVC4應用程序驗證應用程序塊
- 19. 使用鈦製作測驗應用程序的邏輯
- 20. 在DataGridColumn中使用RadioButton和RadioButtonGroup(在測驗應用程序中)
- 21. 使用PHP和MySQL填充測驗Web應用程序表格
- 22. 使用webrat的sinatra應用程序的驗收測試失敗
- 23. 使用C#和數據庫創建測驗應用程序
- 24. 移動應用程序測試遠程測試實驗室
- 25. 使用OAuth驗證javascript應用程序
- 26. 使用DDMS驗證應用程序。 Android
- 27. 使用Google驗證Heroku應用程序
- 28. Robolectric未使用測試應用程序
- 29. 使用Zombie測試Node.js應用程序
- 30. 使用Appium測試NativeScript應用程序
這兩個會的工作。 –