我正在尝试创建一个应用程序,它将使用我已经创建的SQLite数据库并能够读取/写入该数据库。我跟随this site.when我尝试运行我的应用程序并插入或读取数据库中的数据。 LogCat一直告诉我,没有名为User的表,这是我知道我创建的数据库中的一个表,并放入项目中的assets资源文件夹中
11-28 08:15:06.469: E/SQLiteLog(1945): (1) no such table: User
我创建了一个也包含SQLiteHelper嵌套类的Adapter。 任何人都可以帮我解决这个错误吗?我怎么处理它? 的 EDIT2
public class DatabaseAdapter {
private final String TAG = "DatabaseAdapter";
private DatabaseOpenHelper openHelper;
public DatabaseAdapter(Context context) {
openHelper = new DatabaseOpenHelper(context, "DietDatabase.db",
null, 1);
}
public DatabaseAdapter createDatabase() throws SQLException
{
try
{
openHelper.createDataBase();
}
catch (IOException mIOException)
{
Log.e(TAG, mIOException.toString() + " UnableToCreateDatabase");
throw new Error("UnableToCreateDatabase");
}
return this;
}
public DatabaseAdapter open() throws SQLException
{
try
{
openHelper.openDataBase();
openHelper.close();
mDb = openHelper.getReadableDatabase();
}
catch (SQLException mSQLException)
{
Log.e(TAG, "open >>"+ mSQLException.toString());
throw mSQLException;
}
return this;
}
public void close()
{
openHelper.close();
}
// for User table ==================================================
public static final String TABLE_USER = "User";
public static final String TABLE_USER_ID = "_id";
public static final String TABLE_USER_HEIGHT = "_height";
public static final String TABLE_USER_WEIGHT = "_weight";
public static final String TABLE_USER_AGE = "_age";
public static final String TABLE_USER_GENDER = "_gender";
public static final String TABLE_USER_PA = "_pa";
public static final String TABLE_USER_BMIVALUE = "_bmivalue";
public static final String TABLE_USER_BMIINTERPRETATION = "_bmiInterpretation";
public static final String TABLE_USER_IDEALWEIGHT = "_idealweight";
public static final String TABLE_USER_DAILYCALORIES = "_dailycalories";
public int getuserid(){
int id;
SQLiteDatabase myDataBase = null;
myDataBase = openHelper.getWritableDatabase();
Cursor cur = myDataBase.query(TABLE_USER,
null, null, null, null, null, null);
cur.moveToFirst();
id=cur.getInt(0);
cur.close();
return id;}
// insert into user table======================================================
public Long insertUser(User user) {
SQLiteDatabase myDataBase = null;
Long id = -1L;
try {
ContentValues values = new ContentValues();
values.put(TABLE_USER_HEIGHT, user.getHeight());
values.put(TABLE_USER_WEIGHT, user.getWeight());
values.put(TABLE_USER_AGE, user.getAge());
values.put(TABLE_USER_GENDER, user.getGender());
values.put(TABLE_USER_PA, user.getPa());
values.put(TABLE_USER_BMIVALUE, user.getBmivalue());
values.put(TABLE_USER_BMIINTERPRETATION,
user.getBmiInterpretation());
values.put(TABLE_USER_IDEALWEIGHT, user.getIdealweight());
values.put(TABLE_USER_DAILYCALORIES, user.getDailycalories());
myDataBase = openHelper.getWritableDatabase();
id = myDataBase.insert(TABLE_USER, null, values);
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
} finally {
if (myDataBase != null && myDataBase.isOpen())
myDataBase.close();
}
return id;
}
//databaseOpenHelper=========================================================
static class DatabaseOpenHelper extends SQLiteOpenHelper {
private static String TAG1 = "DatabaseOpenHelper";
public static File DB_PATH ;
private static String DB_NAME = "DietDatabase.db";
private SQLiteDatabase myDataBase;
private final Context myContext;
public DatabaseOpenHelper(Context context, String name,
CursorFactory factory, int version) {
super(context, DB_NAME, null, 1);
DB_PATH=context.getDatabasePath(DB_NAME);
this.myContext = context;
}
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
if (dbExist) {
} else {
this.getReadableDatabase();
this.close();
try {
copyDataBase();
Log.e(TAG1, "createDatabase database created");
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
private boolean checkDataBase() {
File dbFile = new File(DB_PATH + DB_NAME);
//Log.v("dbFile", dbFile + " "+ dbFile.exists());
return dbFile.exists();
}
private void copyDataBase() throws IOException {
// Open local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
// Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
// transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
// Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDataBase() throws SQLException {
// Open the database
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.CREATE_IF_NECESSARY);
}
@Override
public synchronized void close() {
if (myDataBase != null)
myDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
}
我在哪里使用数据库
DatabaseAdapter dbAdapter = new DatabaseAdapter(BmiCalculaterPage.this);
dbAdapter.createDatabase();
dbAdapter.open();
dbAdapter.insertUser(user);
int x=dbAdapter.getuserid();
dbAdapter.close();
当我跟踪它时,我发现应用程序在尝试返回x的值时停止了但我无法找到为什么会产生这种情况?
答案 0 :(得分:1)
你只是在写
super(context, DB_NAME, null, 1);
{p}在DatabaseOpenHelper
的构造函数中。
此处不是DB_NAME,您需要提供如下所示的路径+ DB_NAME,
super(context, EXTERNAL_PATH + "\" + DB_NAME, null, 1);
现在它将指向外部存储中的数据库。以前,代码在本地应用程序路径中搜索DB。