我尝试写入加密(SQLCipher)数据库中的现有表,但我收到错误消息“net.sqlcipher.database.SQLiteException:没有这样的表”。我该怎么做?
我已经通过 SQLite studio 在我的项目的 assets 文件夹中创建了一个 SQLCipher 数据库和表。然后我尝试写入表格。
每次应用程序启动时都会更新创建的表?
DatabaseHelper 类中的方法:
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.util.Log;
import com.readystatesoftware.sqliteasset.SQLiteAssetHelper;
import net.sqlcipher.database.SQLiteDatabase;
import java.io.File;
public class DatabaseHelper extends SQLiteAssetHelper {
public DatabaseHelper(Context context, String dbName) {
super(context, dbName, null, 1);
}
//Command Out
/* public void InitializeSQLCipher(File databaseFile, String dbPass, String tableName) {
databaseFile.mkdirs();
databaseFile.delete();
SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(databaseFile, dbPass, null);
database.execSQL("create table " + tableName + "( ID INTEGER PRIMARY KEY AUTOINCREMENT, columnOne TEXT, columnTwo TEXT)");
database.close();
}*/
public void FillSQLCipher(File databaseFile, String dbPass, String tableName, String columnOne, String columnTwo, String values) {
SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(databaseFile, dbPass, null);
ContentValues cValues = new ContentValues();
cValues.put(columnOne, values);
cValues.put(columnTwo, values);
database.insert(tableName, null, cValues);
database.close();
}
public void getDataFromTable(File databaseFile, String dbPass, String tableName) {
try {
SQLiteDatabase enDatabase = SQLiteDatabase.openOrCreateDatabase(databaseFile, dbPass, null);
Cursor cursor = enDatabase.rawQuery("select * from " + tableName, null);
if (cursor.moveToFirst()) {
do {
for (int i = 0; i < cursor.getColumnCount(); i++) {
String neu = cursor.getString(i);
Log.d("Data: ", neu + "");
}
} while (cursor.moveToNext());
cursor.close();
enDatabase.close();
}
cursor.close();
enDatabase.close();
}catch (Exception e){
Log.d(tableName, "can not read the table!");
}
}
public void cleanData(File databaseFile, String dbPass, String tableName) {
SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(databaseFile, dbPass, null);
database.execSQL("delete from " +tableName);
database.close();
}
}
//和我的活动
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import net.sqlcipher.database.SQLiteDatabase;
import java.io.File;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SQLiteDatabase.loadLibs(this);
Date now = new Date();
Calendar calendar = Calendar.getInstance();
calendar.getTimeInMillis();
//time now
Log.d("** Zeit jetzt: ", String.valueOf(now));
Log.d("** Milisekunden: ", String.valueOf(calendar.getTimeInMillis()));
***************************************\n");
Log.d("** verschlüsselte", "Datenbank:\n");
//Fill the table in the encrypted database
DatabaseHelper enDBHelper = new EncryptedDatabaseHelper(this, "encryptedDB.db");
File databaseFile = getDatabasePath("encryptedDB.db");
enDBHelper.InitializeSQLCipher(databaseFile, "abc123", "encryptedTableOne");
for (int i = 0; i < 21; i++) {
int randomValue = new Random().nextInt();
enDBHelper.FillSQLCipher(databaseFile, "abc123", "encryptedTableOne", "ColumnOne", "ColumnTwo", String.valueOf(randomValue));
}
//time now
now = new Date();
calendar = Calendar.getInstance();
Log.d("** Zeit jetzt: ", String.valueOf(now));
Log.d("** Milisekunden: ", String.valueOf(calendar.getTimeInMillis()));
Log.d("\n", " daten aus der verschlüsselten Datenbank ***************************************\n");
//read the table from encrypted database!
enDBHelper.getDataFromTable(databaseFile, "abc123", "encryptedTableOne");
/* enDBHelper.cleanData(databaseFile, "abc123", "encryptedTableOne");*/
Log.d("\n*******************", "\n");
now = new Date();
calendar = Calendar.getInstance();
Log.d("** Zeit jetzt: ", String.valueOf(now));
Log.d("** Milisekunden: ", String.valueOf(calendar.getTimeInMillis()));
Log.d("\n", " und hier ist Ende ***************************************\n");
}
}