我的代码需要帮助。我试图将文本保存到SQLite数据库,但它说没有这样的表,虽然我已经声明。
WordDatabase.java
package com.fyp.atms;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class WordDatabase {
public static final String WORD_ID = "_id";
public static final String WORD_TOTRANSLATE = "to_translate";
public static final String WORD_TRANSLATED = "translated";
private static final String DATABASE_NAME = "WordHistory";
private static final String DATABASE_TABLE = "WordTable";
private static final int DATABASE_VERSION = 1;
private Database db;
private final Context cont;
private SQLiteDatabase HistoryDatabase;
public static class Database extends SQLiteOpenHelper{
public Database(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
WORD_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
WORD_TOTRANSLATE + " TEXT NOT NULL, " +
WORD_TRANSLATED + " TEXT NOT NULL);"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
}
public WordDatabase(Context con){
cont = con;
}
public WordDatabase open() throws Exception{
db = new Database(cont);
HistoryDatabase = db.getWritableDatabase();
return this;
}
public void close(){
db.close();
}
public void createEntry(String beforeTranslate, String afterTranslate) {
// TODO Auto-generated method stub
ContentValues content = new ContentValues();
content.put(WORD_TOTRANSLATE, beforeTranslate);
content.put(WORD_TRANSLATED, afterTranslate);
HistoryDatabase.insert(DATABASE_TABLE, null, content);
}
public String getData() {
// TODO Auto-generated method stub
String[] col = new String[]{WORD_ID, WORD_TOTRANSLATE, WORD_TRANSLATED};
Cursor curs = HistoryDatabase.query(DATABASE_TABLE, col, null, null, null, null, null);
String res = "";
int rowNum = curs.getColumnIndex(WORD_ID);
int toTranslateNum = curs.getColumnIndex(WORD_TOTRANSLATE);
int TranslatedNum = curs.getColumnIndex(WORD_TRANSLATED);
for(curs.moveToFirst(); !curs.isAfterLast(); curs.moveToNext()){
res = res + curs.getString(rowNum) + " " + curs.getString(toTranslateNum) + " " + curs.getString(TranslatedNum) + "\n";
}
return res;
}
}
MainActivity.java - onClickListener
try{
String beforeTranslate = toTl.getText().toString();
String afterTranslate = tled.getText().toString();
WordDatabase newEntry = new WordDatabase(MainActivity.this);
newEntry.open();
newEntry.createEntry(beforeTranslate, afterTranslate);
newEntry.close();
}catch(Exception e){
e.printStackTrace();
}
Wordhistory.java
package com.fyp.atms;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Wordhistory extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.wordhistory);
TextView d = (TextView) findViewById(R.id.HistoryData);
WordDatabase words = new WordDatabase(this);
try{
words.open();
String data = words.getData();
words.close();
d.setText(data);
}catch(Exception e){
e.printStackTrace();
}
}
}
这是LogCat
10-22 01:33:51.884: E/SQLiteLog(25675): (1) no such table: WordTable
10-22 01:33:51.884: E/SQLiteDatabase(25675): Error inserting translated=你好 to_translate=hello
10-22 01:33:51.884: E/SQLiteDatabase(25675): android.database.sqlite.SQLiteException: no such table: WordTable (code 1): , while compiling: INSERT INTO WordTable(translated,to_translate) VALUES (?,?)
10-22 01:33:51.884: E/SQLiteDatabase(25675): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
10-22 01:33:51.884: E/SQLiteDatabase(25675): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
10-22 01:33:51.884: E/SQLiteDatabase(25675): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
10-22 01:33:51.884: E/SQLiteDatabase(25675): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
10-22 01:33:51.884: E/SQLiteDatabase(25675): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
10-22 01:33:51.884: E/SQLiteDatabase(25675): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
10-22 01:33:51.884: E/SQLiteDatabase(25675): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
10-22 01:33:51.884: E/SQLiteDatabase(25675): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
10-22 01:33:51.884: E/SQLiteDatabase(25675): at com.fyp.atms.WordDatabase.createEntry(WordDatabase.java:70)
10-22 01:33:51.884: E/SQLiteDatabase(25675): at com.fyp.atms.MainActivity$1.onClick(MainActivity.java:109)
10-22 01:33:51.884: E/SQLiteDatabase(25675): at android.view.View.performClick(View.java:4447)
10-22 01:33:51.884: E/SQLiteDatabase(25675): at android.view.View$PerformClick.run(View.java:18445)
10-22 01:33:51.884: E/SQLiteDatabase(25675): at android.os.Handler.handleCallback(Handler.java:733)
10-22 01:33:51.884: E/SQLiteDatabase(25675): at android.os.Handler.dispatchMessage(Handler.java:95)
10-22 01:33:51.884: E/SQLiteDatabase(25675): at android.os.Looper.loop(Looper.java:136)
10-22 01:33:51.884: E/SQLiteDatabase(25675): at android.app.ActivityThread.main(ActivityThread.java:5050)
10-22 01:33:51.884: E/SQLiteDatabase(25675): at java.lang.reflect.Method.invokeNative(Native Method)
10-22 01:33:51.884: E/SQLiteDatabase(25675): at java.lang.reflect.Method.invoke(Method.java:515)
10-22 01:33:51.884: E/SQLiteDatabase(25675): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:805)
10-22 01:33:51.884: E/SQLiteDatabase(25675): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:621)
10-22 01:33:51.884: E/SQLiteDatabase(25675): at dalvik.system.NativeStart.main(Native Method)
10-22 01:34:57.564: I/MiuiCursorController(25675): InsertionPointCursorController is hidden including all panels and handle