我尝试使用Sqlite数据库浏览器进行登录,该浏览器基本上位于资产文件夹中。每当我登录时,应用程序总是关闭,收到关于没有登录这样的表的错误。
在我的数据库broswer中有这样一个表但是在ddms中,只显示了android_metadata,并且不存在登录这样的表。请帮帮我。
这是我的DatabaseHelper
public class DatabaseHelper extends SQLiteOpenHelper {
private static String DB_NAME = "cafs";
private static int DB_Version = 2;
private SQLiteDatabase db;
private final Context context;
private String DB_PATH = "/data/data/com.example.trialngiba/databases/";
public DatabaseHelper(Context context) {
super(context, DB_NAME, null, DB_Version);
this.context = context;
}
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
if (dbExist) {
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
} else {
SQLiteDatabase db = this.getWritableDatabase();
if (db.isOpen()){
db.close();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
} else {
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
}
private boolean checkDataBase() {
File dbFile = new File(DB_PATH + DB_NAME);
return dbFile.exists();
}
private void copyDataBase() throws IOException {
InputStream myInput = context.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
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 DatabaseHelper open() throws SQLException
{
db = this.getWritableDatabase();
return this;
}
public void close()
{
db.close();
}
public SQLiteDatabase getDatabaseInstance()
{
return db;
}
public void insertEntry(String userName,String password)
{
ContentValues newValues = new ContentValues();
newValues.put("USERNAME", userName);
newValues.put("PASSWORD",password);
db.insert("login", null, newValues);
}
public int deleteEntry(String UserName)
{
String where="USERNAME=?";
int numberOFEntriesDeleted= db.delete("login", where, new String[]{UserName}) ;
return numberOFEntriesDeleted;
}
public String getSinlgeEntry(String userName)
{
Cursor cursor=db.query("login", null, " USERNAME=?", new String[]{userName}, null, null, null);
if(cursor.getCount()<1)
{
cursor.close();
return "NOT EXIST";
}
cursor.moveToFirst();
String password= cursor.getString(cursor.getColumnIndex("PASSWORD"));
cursor.close();
return password;
}
public void updateEntry(String userName,String password)
{
ContentValues updatedValues = new ContentValues();
updatedValues.put("USERNAME", userName);
updatedValues.put("PASSWORD",password);
String where="USERNAME = ?";
db.update("login",updatedValues, where, new String[]{userName});
}
@Override
public void onCreate(SQLiteDatabase arg0) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
Log.d ("onUpgrade first log", Integer.toString(db.getVersion()));
if (oldVersion == 1) {
DB_Version = 2;
db.setVersion(2);
Log.d ("onUpgrade second log", Integer.toString(db.getVersion()));
}
else {
Log.d("onUpgrade", "else-clause: Already upgraded!");
}
}
}
这是我的logcat
12-01 00:58:02.408: E/Trace(3081): error opening trace file: No such file or directory (2)
12-01 00:58:09.822: E/SQLiteLog(3081): (1) no such table: login
12-01 00:58:09.851: E/AndroidRuntime(3081): FATAL EXCEPTION: main
12-01 00:58:09.851: E/AndroidRuntime(3081): android.database.sqlite.SQLiteException: no such table: login (code 1): , while compiling: SELECT * FROM login WHERE USERNAME=?
12-01 00:58:09.851: E/AndroidRuntime(3081): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
12-01 00:58:09.851: E/AndroidRuntime(3081): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
12-01 00:58:09.851: E/AndroidRuntime(3081): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
12-01 00:58:09.851: E/AndroidRuntime(3081): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
12-01 00:58:09.851: E/AndroidRuntime(3081): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
12-01 00:58:09.851: E/AndroidRuntime(3081): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
12-01 00:58:09.851: E/AndroidRuntime(3081): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
12-01 00:58:09.851: E/AndroidRuntime(3081): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
12-01 00:58:09.851: E/AndroidRuntime(3081): at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161)
12-01 00:58:09.851: E/AndroidRuntime(3081): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032)
12-01 00:58:09.851: E/AndroidRuntime(3081): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1200)
12-01 00:58:09.851: E/AndroidRuntime(3081): at com.example.trialngiba.DatabaseHelper.getSinlgeEntry(DatabaseHelper.java:131)
12-01 00:58:09.851: E/AndroidRuntime(3081): at com.example.trialngiba.MainActivity$2.onClick(MainActivity.java:67)
12-01 00:58:09.851: E/AndroidRuntime(3081): at android.view.View.performClick(View.java:4091)
12-01 00:58:09.851: E/AndroidRuntime(3081): at android.view.View$PerformClick.run(View.java:17072)
12-01 00:58:09.851: E/AndroidRuntime(3081): at android.os.Handler.handleCallback(Handler.java:615)
12-01 00:58:09.851: E/AndroidRuntime(3081): at android.os.Handler.dispatchMessage(Handler.java:92)
12-01 00:58:09.851: E/AndroidRuntime(3081): at android.os.Looper.loop(Looper.java:153)
12-01 00:58:09.851: E/AndroidRuntime(3081): at android.app.ActivityThread.main(ActivityThread.java:5042)
12-01 00:58:09.851: E/AndroidRuntime(3081): at java.lang.reflect.Method.invokeNative(Native Method)
12-01 00:58:09.851: E/AndroidRuntime(3081): at java.lang.reflect.Method.invoke(Method.java:511)
12-01 00:58:09.851: E/AndroidRuntime(3081): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
12-01 00:58:09.851: E/AndroidRuntime(3081): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
12-01 00:58:09.851: E/AndroidRuntime(3081): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
我想你忘了关闭myOutput,系统会删除半写文件。
查看文档http://developer.android.com/reference/java/io/OutputStream.html
并尝试将flush()和close()添加到您的代码中。