我有一个现有的SQLite数据库,我想使用我的Android应用程序。我已经查看了所有在线教程,这些教程对我不起作用。我在模拟器中启动应用程序时出现错误,我在数据库中创建的表不存在*(android.database.sqlite.SQLiteException:没有这样的表:adk_peaks(代码1):,同时编译: SELECT * FROM adk_peaks WHERE 1)*。
adk_peaks是表的名称。现在我只是试图从表中获取信息以显示在TextView中。
以下是我对SQLiteOpenHelper的扩展:
package kyle.peaktracker;
import android.database.SQLException;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase;
import android.database.Cursor;
import android.content.Context;
import android.content.ContentValues;
import android.util.Log;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class MyDBHandler extends SQLiteOpenHelper {
public static final int DATABASE_VERSION = 2;
private static final String DATABASE_NAME = "peaks.db";
//private static final String DATABASE_PATH = "/app/src/main/assests/Database/";
//private static final String DATABASE_PATH = "/data/data/kyle.peaktracker/databases/";
//private static final String DATABASE_PATH = "\\app\\src\\main\\assets\\Database\\";
public static String DATABASE_PATH = ""; //final?
private static final String TABLE_ADK = "adk_peaks";
private SQLiteDatabase myDataBase;
private final Context myContext;
//Columns
private static final String COLUMN_ID = "_id";
private static final String COLUMN_NAME = "_name";
private static final String COLUMN_HEIGHT = "_height";
private static final String COLUMN_CLIMBED = "_climbed";
private static final String COLUMN_DATE = "_date";
private static final String COLUMN_LIST = "_list";
public MyDBHandler(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.myContext = context;
DATABASE_PATH = myContext.getDatabasePath(DATABASE_NAME).toString();
}
public void createDatabase() throws IOException
{
boolean dbExist = checkDataBase();
if(dbExist)
{
Log.v("DB Exists", "db exists");
}
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;
File dbfile = new File(myPath);
checkDB = dbfile.exists();
}
catch(SQLiteException e)
{
}
return checkDB;
}
private void copyDataBase() throws IOException
{
String outFileName = DATABASE_PATH;
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();
}
public void db_delete()
{
File file = new File(DATABASE_PATH);
if(file.exists())
{
file.delete();
System.out.println("delete database file.");
}
}
//Open database
public void openDatabase() throws SQLException
{
String myPath = DATABASE_PATH;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}
public synchronized void closeDataBase()throws SQLException
{
if(myDataBase != null)
myDataBase.close();
super.close();
}
//Override onCreate and onUpgrade
public void onCreate(SQLiteDatabase db){
/*String query = "CREATE TABLE " + TABLE_ADK + "(" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT " +
COLUMN_NAME + " TEXT " +
COLUMN_HEIGHT + " INTEGER " +
COLUMN_CLIMBED + " INTEGER " +
COLUMN_DATE + " TEXT " +
COLUMN_LIST + " TEXT " +
");";
db.execSQL(query); */
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_ADK);
onCreate(db);
}
public String databaseToString(){
String dbString = "";
SQLiteDatabase db = getWritableDatabase();
String query = "SELECT * FROM " + TABLE_ADK + " WHERE 1";
Cursor c = db.rawQuery(query, null);
c.moveToFirst();
while(!c.isAfterLast()){
if(c.getString(c.getColumnIndex("_name"))!= null){
dbString += c.getString(c.getColumnIndex("_name"));
dbString += "\n";
}
}
c.close(); //Added
db.close();
return dbString;
}
}
这是我的MainActivity: 包kyle.peaktracker;
import android.database.SQLException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import java.io.IOException;
import java.util.List;
public class MainActivity extends AppCompatActivity {
TextView testText;
MyDBHandler dbHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
testText = (TextView) findViewById(R.id.test_text);
//dbHandler = new MyDBHandler(this, null, null, 1);
dbHandler = new MyDBHandler(this);
try {
dbHandler.createDatabase();
} catch (IOException ioe){
throw new Error("Unable to create database");
}
try {
dbHandler.openDatabase();
}catch(SQLException sqle){
throw sqle;
}
printDatabase();
}
public void printDatabase(){
String dbString = dbHandler.databaseToString();
testText.setText(dbString);
}
}
感谢任何建议!
答案 0 :(得分:0)
我认为您的问题是由于使用: -
if(!dbExist1)
{
this.getReadableDatabase();
try
{
this.close();
copyDataBase();
}
catch (IOException e)
{
throw new Error("Error copying database");
}
}
这将导致在调用getReadableDatabase
时创建数据库。然后数据库将为空(表少)。
因此,请将上述内容更改为: -
if(!dbExist1)
{
try
{
copyDataBase();
}
catch (IOException e)
{
throw new Error("Error copying database");
}
}
在运行修改后的代码之前,您需要删除应用程序的数据或卸载应用程序,因为数据库将存在。
以下是我放在一起扩展SQLiteOpenHelper并编写的内容,用于理解从资产中打开数据库的功能(如果数据库存在,则不是这样)。因此它确实有相当广泛的记录,并且有点啰嗦: -
/**
* Database Helper that includes ability to open database from assets
* if the database doesn't exist.
* (i.e. a pre-defined database)
*
*/
public class OpenAssetDBHelper extends SQLiteOpenHelper {
private static final String LOGTAG = "OADB-HLPR";
public static final int MAXIMUM_HELPERS = 10;
private String mDBPath, mAssetPath;
private static OpenAssetDBHelper mInstance[] = new OpenAssetDBHelper[MAXIMUM_HELPERS];
private SQLiteDatabase mDB;
/**
* OpenAssetDBHelper Class that will copy a predefined database
* from the assets folder and then open it is the database;
*
* The copy will only be done if the database does not exist.
*
* Note this code is intended to be used for development and/or
* experimentation, hence the extensive logging.
*/
/**
* get an OpenAssetDBHelper instance as a singleton;
* Note! caters for up to 10 OpenAssetDBHelpers for up to 10 databases
* as specified by the helper_index
*
* @param helper_index Index to this instance/database
* (0-MAXIMUM_HELPERS less 1)
* @param context Context for the database
* @param database Database name (i.e. file name)
* @param dbversion Database version (user_version)
* @param assetfile Name of the asset file to be copied to the database
* @param asset_sub_directories String Array of the sub-directories where
* the asset file is located;
* MUST be in order
* @return The resultant OpenAssetDBHelper
*/
public static synchronized OpenAssetDBHelper getInstance(
int helper_index,
Context context,
String database,
int dbversion,
String assetfile,
String[] asset_sub_directories) {
// Checck that helper_index is within bounds
if (helper_index > (MAXIMUM_HELPERS -1)) {
throw new RuntimeException(
"Helper Index greater than " +
MAXIMUM_HELPERS
);
}
if (helper_index < 0) {
throw new RuntimeException(
"Helper Index cannot be negative, must be 0-" +
MAXIMUM_HELPERS
);
}
// Create the respective OpenAssetDBHelper instance
if(mInstance[helper_index] == null) {
mInstance[helper_index] = new OpenAssetDBHelper(context,
database,
dbversion,
assetfile,
asset_sub_directories);
}
return mInstance[helper_index];
}
/**
* Construct an OpenAssetDBHelper instance;
* Note! can only be called within class
*
* @param context the context to be used
* @param database the database name (equates to filename)
* @param dbversion the databaae version (user_version)
* @param assetfile The name of the asset file i.e. the pre-defined db
* @param directories The hierarchy of directories within the assets folder
* where the asset file is located
* (null or zero elements = in the assets folder)
*/
private OpenAssetDBHelper(Context context,
String database,
int dbversion,
String assetfile,
String[] directories) {
super(context, database, null, dbversion);
Log.d(LOGTAG,"OpenAssetDBHelper being constructed.");
mDBPath = context.getDatabasePath(database).getPath();
if (assetfile == null || assetfile.length() < 1) {
assetfile = database;
}
mAssetPath = buildAssetPath(directories,assetfile);
if (!ifDatabaseExists(mDBPath)) {
Log.d(LOGTAG,"Database " + database + " not found at " + mDBPath + " so attempting to copy from assets.");
if (copyDatabaseFromAssets(context,mDBPath, database, mAssetPath)) {
Log.d(LOGTAG, "Successfully Copied Database from Assets.");
} else {
throw new RuntimeException("No Database Available.");
}
}
// Force Database open and store it
this.mDB = this.getWritableDatabase();
logDatabaseTableInformation(mDB);
Log.d(LOGTAG,"OpenAssetDBHelper constructed.");
}
/**
* onCreate - This is where you would create tables;
* Typically this is where you would alter the structure of the database;
* Note that this is called once for the lifetime of the database.
* @param db The SQLitedatabase
*/
@Override
public void onCreate(SQLiteDatabase db) {
Log.d(LOGTAG,new Object(){}.getClass().getEnclosingMethod().getName() + " initiated.");
// As Database is copied from assets nothing to do in onCreate!
Log.d(LOGTAG,new Object(){}.getClass().getEnclosingMethod().getName() + " completed.");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.d(LOGTAG,new Object(){}.getClass().getEnclosingMethod().getName() + " initiated.");
// Nothing to do as it's early days in the Database's lifetime.
Log.d(LOGTAG,new Object(){}.getClass().getEnclosingMethod().getName() + " completed.");
}
/**
* Check to see if the Database exists,
* if it doesn't exists then check to see if
* the database directory exists,
* if the directory(ies) does(do) not exist then make the directory(ies);
*
*
* @param dbpath The path to the database
* @return true if the database exists, else false
*/
private boolean ifDatabaseExists(String dbpath) {
Log.d(LOGTAG,new Object(){}.getClass().getEnclosingMethod().getName() + " initiated.");
File db = new File(dbpath);
if(db.exists()) return true;
File dir = new File(db.getParent());
if (!dir.exists()) {
dir.mkdirs();
}
return false;
}
/**
* Copy the Database from the assets folder
* @param context
* @param dbpath
* @return
*/
private boolean copyDatabaseFromAssets(Context context,String dbpath, String dbname, String asset) {
String thisclass = new Object(){}.getClass().getEnclosingMethod().getName();
Log.d(LOGTAG,thisclass + " initiated");
InputStream assetsdb;
OutputStream database;
File db = new File(dbpath);
int filesize;
// Get the asset file
try {
Log.d(LOGTAG,thisclass + " attempting to find asset " + asset);
assetsdb = context.getAssets().open(asset);
filesize = assetsdb.available();
Log.d(LOGTAG,thisclass + " asset " + asset +
" located successfully with a size of " +
Integer.toString(filesize)
);
} catch (IOException e) {
Log.d(LOGTAG,thisclass + " Did not locate asset " + asset);
e.printStackTrace();
return false;
}
// Read the first 16 bytes from the asset file
byte[] dbcheck = new byte[16];
try {
assetsdb.read(dbcheck,0,16);
} catch (IOException e) {
Log.d(LOGTAG, thisclass + " failed trying to read 16 bytes to check for a valid database. ");
e.printStackTrace();
return false;
}
// Check that the asset file is an SQLite database
String chkdb = new String(dbcheck);
if(!chkdb.equals("SQLite format 3\u0000")) {
Log.d(LOGTAG,thisclass + " asset " +
asset +
" is not a valid SQLite Database File (found " +
chkdb +
" at bytes 1-16 instead of SQLite format 3)");
try {
assetsdb.close();
} catch (IOException e) {
// Not worth doing anything
}
return false;
}
// Close the asset file
try {
assetsdb.close();
} catch (IOException e) {
Log.d(LOGTAG,thisclass +
" failed to close assets file after checking for a valid database."
);
return false;
}
// Re-open the asset file
try {
assetsdb = context.getAssets().open(asset);
filesize = assetsdb.available();
} catch (IOException e) {
Log.d(LOGTAG, thisclass +
" failed trying to re-open asset " +
asset +
" after checking for a valid database."
);
e.printStackTrace();
return false;
}
// Read the entire asset file into a buffer
Log.d(LOGTAG, thisclass +
" copying asset database " +
dbname +
" into buffer of size " +
filesize
);
byte[] buffer = new byte[filesize];
// Close the asset file
try {
assetsdb.read(buffer);
Log.d(LOGTAG,thisclass +
" closing asset database " + dbname
);
assetsdb.close();
} catch (IOException e) {
Log.d(LOGTAG, thisclass +
" failed while copying asset database " +
dbname +
" (or closing asset database)."
);
e.printStackTrace();
return false;
}
// Open the new database file
try {
Log.d(LOGTAG,thisclass + " attempting to open new database file " + dbpath);
database = new FileOutputStream(dbpath);
} catch (IOException e) {
Log.d(LOGTAG, thisclass + " failed to open new database file.");
e.printStackTrace();
return false;
}
// Write the new database file
try {
Log.d(LOGTAG, thisclass + " writing new database file " + dbpath);
database.write(buffer);
} catch (IOException e) {
Log.d(LOGTAG, thisclass + " failed while writing new database file " + dbpath);
e.printStackTrace();
return false;
}
// Flush the new database file
try {
Log.d(LOGTAG, thisclass + " flushing new database file " + dbpath);
database.flush();
} catch (IOException e) {
Log.d(LOGTAG, thisclass + " failed while flushing new database file " + dbpath);
e.printStackTrace();
return false;
}
// Close the new database file
try {
Log.d(LOGTAG, thisclass + " closing new database file " + dbpath);
database.close();
} catch (IOException e) {
Log.d(LOGTAG, thisclass + " failed while closing new database file " + dbpath);
e.printStackTrace();
return false;
}
Log.d(LOGTAG,new Object(){}.getClass().getEnclosingMethod().getName() + " completed.");
return true;
}
/**
* Log Database table Information
*/
private void logDatabaseTableInformation(SQLiteDatabase db) {
Log.d(LOGTAG,new Object(){}.getClass().getEnclosingMethod().getName() + " initiated.");
String mastertable = "sqlite_master";
String typecolumn = "type";
String namecolumn = "name";
String sqlcolumn = "sql";
String[] args = new String[]{"table","android_metadata"};
Cursor csr = db.query(mastertable,
null,
typecolumn + "=? AND " + namecolumn + "!=?",
args,
null,null,null
);
while (csr.moveToNext()) {
Log.d(LOGTAG,"Database contains Table " +
csr.getString(csr.getColumnIndex(namecolumn)) +
" created by SQL " +
csr.getString(csr.getColumnIndex(sqlcolumn))
);
logTableInformation(db, csr.getString(csr.getColumnIndex(namecolumn)));
}
csr.close();
Log.d(LOGTAG,new Object(){}.getClass().getEnclosingMethod().getName() + " completed.");
}
/**
* Write Table information, Table name, Column Count,
* Row Count and Column Names to the Log
* @param table Name of the table to be reported on
*/
private void logTableInformation(SQLiteDatabase db, String table) {
Cursor csr = db.query(table,
null,
null,
null,
null,
null,
null
);
Log.d(LOGTAG,"Table is " +
table +
" Column Count = " +
Integer.toString(csr.getColumnCount()) +
" Row Count = " +
Long.toString(DatabaseUtils.queryNumEntries(mDB,table))
);
StringBuilder columns_as_string = new StringBuilder();
for (String s: csr.getColumnNames()) {
columns_as_string.append(s).append(" ");
}
Log.d(LOGTAG, "\tColumns are :- " + columns_as_string);
csr.close();
}
/**
* Build the sub-path to the asset, according to the directories specified
*
* @param directories directories underneath the assets folder where
* the asset files is located, null or empty
* array if file is located directly in the
* assets folder;
* directories must be specified in the order
* in which they appear in the path.
* @param filename The filename of the asset
* @return The fill sub-path to the asset
*/
private String buildAssetPath(String[] directories, String filename) {
StringBuilder sb = new StringBuilder();
final String SEPERATOR = "/";
if (directories != null && directories.length > 0) {
for (String s: directories) {
sb.append(s);
if (!s.substring(s.length()-1,s.length()).equals(SEPERATOR)) {
sb.append(SEPERATOR);
}
}
sb.append(filename);
return sb.toString();
} else {
return filename;
}
}
}
用法示例: -
public class MainActivity extends AppCompatActivity {
public static final int DBVERSION = 1;
public static final String DBNAME1 = "openassetdb.db";
public static final int DB1_INDEX = 0;
public static final String DBNAME2 = "myshopping";
public static final int DB2_INDEX = 1;
public static final String DBNAME3 = "rumplestilskin";
public static final int DB3_INDEX = 2;
OpenAssetDBHelper mOADBHlpr;
OpenAssetDBHelper mShoppingHelper;
OpenAssetDBHelper mShopper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Example 1 - Database file located in the assets folder and
// database name same as file name
mOADBHlpr = OpenAssetDBHelper.getInstance(DB1_INDEX,
this,
DBNAME1,
DBVERSION,
DBNAME1,
null
);
// Example 2 - Database file in databases directory of the assets folder
// database name different to the asset filename
mShoppingHelper = OpenAssetDBHelper.getInstance(DB2_INDEX,
this,
DBNAME2,
DBVERSION,
"shopper",
new String[]{"databases"}
);
// Example 3 - Databse file in databases directory of the assets folder
// database name different to the asset filename
mShopper = OpenAssetDBHelper.getInstance(DB3_INDEX,
this,
DBNAME3,
DBVERSION,
"Shopper.sqlite",
new String[]{"databases"}
);
}
}
这是示例输出: -
01-18 00:57:36.582 3276-3276/? D/OADB-HLPR: OpenAssetDBHelper being constructed.
01-18 00:57:36.582 3276-3276/? D/OADB-HLPR: ifDatabaseExists initiated.
01-18 00:57:36.582 3276-3276/? D/OADB-HLPR: Database rumplestilskin not found at /data/data/mjt.openassetdb/databases/rumplestilskin so attempting to copy from assets.
01-18 00:57:36.582 3276-3276/? D/OADB-HLPR: copyDatabaseFromAssets initiated
01-18 00:57:36.582 3276-3276/? D/OADB-HLPR: copyDatabaseFromAssets attempting to find asset databases/Shopper.sqlite
01-18 00:57:36.582 3276-3276/? D/OADB-HLPR: copyDatabaseFromAssets asset databases/Shopper.sqlite located successfully with a size of 524288
01-18 00:57:36.582 3276-3276/? D/OADB-HLPR: copyDatabaseFromAssets copying asset database rumplestilskin into buffer of size 524288
01-18 00:57:36.586 3276-3276/? D/dalvikvm: GC_FOR_ALLOC freed 295K, 13% free 6197K/7111K, paused 3ms, total 3ms
01-18 00:57:36.586 3276-3276/? I/dalvikvm-heap: Grow heap (frag case) to 6.892MB for 524300-byte allocation
01-18 00:57:36.602 3276-3278/? D/dalvikvm: GC_CONCURRENT freed 7K, 13% free 6702K/7687K, paused 11ms+1ms, total 13ms
01-18 00:57:36.602 3276-3276/? D/OADB-HLPR: copyDatabaseFromAssets closing asset database rumplestilskin
01-18 00:57:36.602 3276-3276/? D/OADB-HLPR: copyDatabaseFromAssets attempting to open new database file /data/data/mjt.openassetdb/databases/rumplestilskin
01-18 00:57:36.602 3276-3276/? D/OADB-HLPR: copyDatabaseFromAssets writing new database file /data/data/mjt.openassetdb/databases/rumplestilskin
01-18 00:57:36.602 3276-3276/? D/OADB-HLPR: copyDatabaseFromAssets flushing new database file /data/data/mjt.openassetdb/databases/rumplestilskin
01-18 00:57:36.606 3276-3276/? D/OADB-HLPR: copyDatabaseFromAssets closing new database file /data/data/mjt.openassetdb/databases/rumplestilskin
01-18 00:57:36.606 3276-3276/? D/OADB-HLPR: copyDatabaseFromAssets completed.
01-18 00:57:36.606 3276-3276/? D/OADB-HLPR: Successfully Copied Database from Assets.
01-18 00:57:36.618 3276-3276/? D/OADB-HLPR: onCreate initiated.
01-18 00:57:36.618 3276-3276/? D/OADB-HLPR: onCreate completed.
01-18 00:57:36.622 3276-3276/? D/OADB-HLPR: logDatabaseTableInformation initiated.
01-18 00:57:36.622 3276-3276/? D/OADB-HLPR: Database contains Table shops created by SQL CREATE TABLE `shops` (`_id` BIGINT(20) NOT NULL PRIMARY KEY , `shopname` TEXT , `shoporder` BIGINT(20) NOT NULL DEFAULT 100 , `shopstreet` TEXT , `shopcity` TEXT , `shopstate` TEXT , `shopphone` TEXT , `shopnotes` TEXT )
01-18 00:57:36.622 3276-3276/? D/OADB-HLPR: Table is shops Column Count = 8 Row Count = 7
01-18 00:57:36.622 3276-3276/? D/OADB-HLPR: Columns are :- _id shopname shoporder shopstreet shopcity shopstate shopphone shopnotes
01-18 00:57:36.626 3276-3276/? D/OADB-HLPR: Database contains Table aisles created by SQL CREATE TABLE `aisles` (`_id` BIGINT(20) NOT NULL PRIMARY KEY , `aislename` TEXT , `aisleorder` BIGINT(20) NOT NULL DEFAULT 100 , `aisleshopref` BIGINT(20) NOT NULL )
01-18 00:57:36.626 3276-3276/? D/OADB-HLPR: Table is aisles Column Count = 4 Row Count = 63
01-18 00:57:36.626 3276-3276/? D/OADB-HLPR: Columns are :- _id aislename aisleorder aisleshopref
01-18 00:57:36.626 3276-3276/? D/OADB-HLPR: Database contains Table products created by SQL CREATE TABLE `products` (`_id` BIGINT(20) NOT NULL PRIMARY KEY , `productname` TEXT , `productorder` BIGINT(20) NOT NULL DEFAULT 100 , `productaisleref` BIGINT(20) NOT NULL , `productuses` BIGINT(20) NOT NULL DEFAULT 0 , `productnotes` TEXT )
01-18 00:57:36.626 3276-3276/? D/OADB-HLPR: Table is products Column Count = 6 Row Count = 106
01-18 00:57:36.626 3276-3276/? D/OADB-HLPR: Columns are :- _id productname productorder productaisleref productuses productnotes
01-18 00:57:36.626 3276-3276/? D/OADB-HLPR: Database contains Table productusage created by SQL CREATE TABLE `productusage` (`productailseref` BIGINT(20) NOT NULL , `productproductref` BIGINT(20) NOT NULL , `productcost` REAL DEFAULT 1.00 , `productbuycount` BIGINT(20) NOT NULL DEFAULT 0 , `productfirstbuydate` BIGINT(20) NOT NULL DEFAULT 0 , `productlatestbuydate` BIGINT(20) NOT NULL DEFAULT 0 , `mincost` REAL , `orderinaisle` BIGINT(20) NOT NULL DEFAULT 100 , PRIMARY KEY (productailseref, productproductref))
01-18 00:57:36.626 3276-3276/? D/OADB-HLPR: Table is productusage Column Count = 8 Row Count = 106
01-18 00:57:36.626 3276-3276/? D/OADB-HLPR: Columns are :- productailseref productproductref productcost productbuycount productfirstbuydate productlatestbuydate mincost orderinaisle
01-18 00:57:36.626 3276-3276/? D/OADB-HLPR: Database contains Table rules created by SQL CREATE TABLE `rules` (`_id` BIGINT(20) NOT NULL PRIMARY KEY , `rulename` TEXT , `ruletype` BIGINT(20) NOT NULL , `rulepromptflag` BIGINT(20) NOT NULL , `ruleperiod` BIGINT(20) NOT NULL , `rulemultiplier` BIGINT(20) NOT NULL , `ruleactiveon` BIGINT(20) NOT NULL , `ruleproductref` BIGINT(20) NOT NULL , `ruleaisleref` BIGINT(20) NOT NULL , `ruleuses` BIGINT(20) NOT NULL DEFAULT 0 , `mincost` REAL DEFAULT 0 , `maxcost` REAL DEFAULT 0 , `rulesnumbettoget` BIGINT(20) NOT NULL DEFAULT 1 )
01-18 00:57:36.626 3276-3276/? D/OADB-HLPR: Table is rules Column Count = 13 Row Count = 24
01-18 00:57:36.626 3276-3276/? D/OADB-HLPR: Columns are :- _id rulename ruletype rulepromptflag ruleperiod rulemultiplier ruleactiveon ruleproductref ruleaisleref ruleuses mincost maxcost rulesnumbettoget
01-18 00:57:36.626 3276-3276/? D/OADB-HLPR: Database contains Table shoplist created by SQL CREATE TABLE `shoplist` (`_id` BIGINT(20) NOT NULL PRIMARY KEY , `slproductid` BIGINT(20) NOT NULL , `sldateadded` BIGINT(20) NOT NULL DEFAULT 0 , `slnumbertoget` BIGINT(20) NOT NULL DEFAULT 1 , `sldone` BIGINT(20) NOT NULL DEFAULT 0 , `sldategot` BIGINT(20) NOT NULL , `slcost` REAL DEFAULT 0 , `productusageref` BIGINT(20) NOT NULL , `aisleref` BIGINT(20) NOT NULL )
01-18 00:57:36.626 3276-3276/? D/OADB-HLPR: Table is shoplist Column Count = 9 Row Count = 485
01-18 00:57:36.626 3276-3276/? D/OADB-HLPR: Columns are :- _id slproductid sldateadded slnumbertoget sldone sldategot slcost productusageref aisleref
01-18 00:57:36.626 3276-3276/? D/OADB-HLPR: Database contains Table appvalues created by SQL CREATE TABLE `appvalues` (`_id` BIGINT(20) NOT NULL PRIMARY KEY , `valuename` TEXT , `valuetype` TEXT , `valueint` BIGINT(20) NOT NULL DEFAULT 0 , `valuereal` REAL DEFAULT '0.0' , `valuetext` TEXT , `valueincludeinsettings` BIGINT(20) NOT NULL DEFAULT 0 , `valuesettingsinfo` TEXT )
01-18 00:57:36.626 3276-3276/? D/OADB-HLPR: Table is appvalues Column Count = 8 Row Count = 7
01-18 00:57:36.626 3276-3276/? D/OADB-HLPR: Columns are :- _id valuename valuetype valueint valuereal valuetext valueincludeinsettings valuesettingsinfo
01-18 00:57:36.626 3276-3276/? D/OADB-HLPR: logDatabaseTableInformation completed.
01-18 00:57:36.626 3276-3276/? D/OADB-HLPR: OpenAssetDBHelper constructed.
答案 1 :(得分:0)
您可以使用任何文件选择器选择数据库文件。 此功能用于替换当前数据库。
此方法适用于我的房间数据库
private void importdb(String db_path) {
try {
File file=new File(db_path);
InputStream mInputStream = new DataInputStream(new FileInputStream(file));
String outFileName = getContext().getDatabasePath(getContext().getResources().getString(R.string.db_name)).getAbsolutePath();
OutputStream mOutputStream = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = mInputStream.read(buffer)) > 0) {
mOutputStream.write(buffer, 0, length);
}
mOutputStream.flush();
mOutputStream.close();
mInputStream.close();
new CustomMessage(getActivity(), "Database replaced sucessfully");
} catch (Exception e) {
e.printStackTrace();
CustomLog.showLogD("WORKING_STOP",e.getMessage());
}
}
答案 2 :(得分:-1)
使用完全限定的路径更改数据库路径,例如: path = context.getApplicationInfo()。dataDir +&#34; / databases /&#34 ;;