eclipse调用数据库错误

时间:2015-11-18 00:41:58

标签: java android eclipse

我想将insert.java类中的单词添加到assets文件夹中的数据库translate.sql,但是当我在模拟器中单击insert.java按钮时,它会意外关闭。

我已经搜索并尝试解决这个问题,但似乎还在继续。

所以我需要帮助来识别代码中的错误或数据库或其他人的一些拼写错误的名称。

这是insert.java

的课程
package com.han;

import android.app.Activity;
//import android.app.AlertDialog;
//import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

        public class Insert extends Activity {
        public Button search, add, back;
        public EditText indo;
        public EditText tora;
        public Cursor mCursor;
        public DbHelper helper;
        public String insertkataindonesia, insertkatatora;
        public int id,temp_id;
        public SQLiteDatabase db = null;
        @Override
         protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_insert);

         helper = new DbHelper(this);
         db = helper.getWritableDatabase();
         mCursor = helper.getAll();
        search = (Button) findViewById(R.id.btnSearch);
         add = (Button) findViewById(R.id.btnAdd);
         back = (Button) findViewById(R.id.btnBack);
         indo = (EditText) findViewById(R.id.insertBahasaIndonesia);
         tora = (EditText) findViewById(R.id.insertBahasaTora);
         add.setEnabled(false);

         search.setOnClickListener(new View.OnClickListener(){
         @Override
         public void onClick (View v){
         String result = "";
         insertkataindonesia = indo.getText().toString().toLowerCase();
         mCursor = db.rawQuery("SELECT _id,kata_indo, kata_tora FROM translate " + "WHERE kata_indo =   '"+insertkataindonesia+"' ORDER BY kata_indo",null);
         if(mCursor.moveToFirst()){
         result = mCursor.getString(2);
         for
        (;!mCursor.isAfterLast();mCursor.moveToNext()){
         result = mCursor.getString(2);
         }
         }
         if (result.equals("")){
         result = "";
         Toast.makeText(Insert.this,"Kata tidak ditemukan, silahkan tambahkan kata", Toast.LENGTH_LONG).show();
         add.setEnabled(true);
         }
         tora.setText(result);
         }});

         add.setOnClickListener(new View.OnClickListener(){
         @Override
         public void onClick (View v){
         insertkataindonesia = indo.getText().toString().toLowerCase();
         insertkatatora =   tora.getText().toString().toLowerCase();
         if (insertkatatora.equals("") && insertkataindonesia.equals("")){
         Toast.makeText(Insert.this,"Tidak ada kata untuk disimpan", Toast.LENGTH_LONG).show();
         }
         else if (insertkatatora.equals("")){
         Toast.makeText(Insert.this,"Tidak ada kata toraja untuk disimpan", Toast.LENGTH_LONG).show();
         }
         else if (insertkataindonesia.equals("")){
         Toast.makeText(Insert.this,"Tidak ada kata Indonesia untuk disimpan", Toast.LENGTH_LONG).show();
         }
         else if (id==-1){
         Toast.makeText(Insert.this,"Kata sudah ada di database, silahkan hapus dulu", Toast.LENGTH_LONG).show();
         }
         else{
         helper.insertKey(insertkataindonesia, insertkatatora);
         Toast.makeText(Insert.this,"Kata telah disimpan", Toast.LENGTH_SHORT).show();
         indo.setText("");
         tora.setText("");
         }

         }


         });

         back.setOnClickListener(new View.OnClickListener(){
         @Override
         public void onClick (View v){
         Intent menu = new Intent(Insert.this,
        MainActivity.class);
         menu.putExtra("pesan", "From Insert Menu");
         startActivity(menu);
         }

         });
    }
}

这是dbhelper.java

的课程
package com.han;

    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import android.content.ContentValues;
    import android.content.Context;
    import android.content.res.AssetManager;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteException;
    import android.database.sqlite.SQLiteOpenHelper;

    public class DbHelper extends SQLiteOpenHelper {

        private static final String DATABASE_NAME="translate.sql";
        public static final String KEY_ROWID = "_id";
        private static final int VERSION = 1;
        private static File DATABASE_FILE;
        private boolean mInvalidDatabaseFile = false;
        private boolean mIsUpgraded = false;
        private Context mContext;
        private int mOpenConnections = 0;
        private static DbHelper mInstance;

        synchronized static public DbHelper getInstance(Context context) {

            if (mInstance == null) {
            mInstance = new
            DbHelper(context.getApplicationContext());
            }
            return mInstance;
            }
            public DbHelper(Context context) {
            super(context, DATABASE_NAME, null, VERSION);
            this.mContext = context;
            SQLiteDatabase db = null;
            try {
            db = getReadableDatabase();
            if (db != null) {
            db.close();
            }
            DATABASE_FILE =
            context.getDatabasePath(DATABASE_NAME);
            if (mInvalidDatabaseFile) {
            copyDatabase();
            }
            if (mIsUpgraded) {
            doUpgrade();

            }
            }catch (SQLiteException e) {
            } finally {
            if (db != null && db.isOpen()) {
            db.close();
            }
            }
            }

            @Override
            public void onCreate(SQLiteDatabase db) {
            mInvalidDatabaseFile = true;
            }

            @Override
            public void onUpgrade(SQLiteDatabase database, int old_version,
            int new_version) {
            mInvalidDatabaseFile = true;
            mIsUpgraded = true;
            }
            private void doUpgrade() {
            }

            @Override
            public synchronized void onOpen(SQLiteDatabase db) {
            super.onOpen(db);
            mOpenConnections++;
            if (!db.isReadOnly()) {
            db.execSQL("PRAGMA foreign_keys=ON;");
            }
            }

            @Override
            public synchronized void close() {
            mOpenConnections--;
            if (mOpenConnections == 0) {
            super.close();
            }
            }

            private void copyDatabase() {
            AssetManager assetManager =
            mContext.getResources().getAssets();
            InputStream in = null;
            OutputStream out = null;

            try {
            in = assetManager.open(DATABASE_NAME);
            out = new FileOutputStream(DATABASE_FILE);
            byte[] buffer = new byte[1024];
            int read = 0;
            while ((read = in.read(buffer)) != -1) {
            out.write(buffer, 0, read);
            }
            } catch (IOException e) {

            } finally {
            if (in != null) {

            try {
            in.close();
            } catch
            (IOException e) {}
            }

            if (out != null) {
            try {
            out.close();
            } catch (IOException e) {}
            }
            }

            setDatabaseVersion();
            mInvalidDatabaseFile = false;
            }

            private void setDatabaseVersion() {
            SQLiteDatabase db = null;
            try {
            db = SQLiteDatabase.openDatabase(DATABASE_FILE.getAbsolutePath(), null, SQLiteDatabase.OPEN_READWRITE);
            db.execSQL("PRAGMA user_version = " + VERSION);
            } catch (SQLiteException e ) {
            } finally {
            if (db != null && db.isOpen()) {
            db.close();
            }
            }
            }

            public Cursor getAll (){
            return(getReadableDatabase().rawQuery("SELECT _id, kata_indo, kata_tora from translate ORDER BY _id ASC",null));
            }

            public void insertKey(String indo, String tora){
            ContentValues cv = new ContentValues();
            cv.put("kata_indo", indo);
            cv.put("kata_tora", tora);
            getWritableDatabase().insert("translate","kata_indo", cv);
            }

            public void delete(long id){
            getWritableDatabase().delete("translate", KEY_ROWID + "=" + id, null);
        }
}

这是logcat

11-18 00:16:38.489: E/AndroidRuntime(396): FATAL EXCEPTION: main
11-18 00:16:38.489: E/AndroidRuntime(396): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.han/com.han.Insert}: android.database.sqlite.SQLiteException: no such table: translate: , while compiling: SELECT _id, kata_indo, kata_tora from translate ORDER BY _id ASC
11-18 00:16:38.489: E/AndroidRuntime(396):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
11-18 00:16:38.489: E/AndroidRuntime(396):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-18 00:16:38.489: E/AndroidRuntime(396):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-18 00:16:38.489: E/AndroidRuntime(396):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-18 00:16:38.489: E/AndroidRuntime(396):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-18 00:16:38.489: E/AndroidRuntime(396):  at android.os.Looper.loop(Looper.java:123)
11-18 00:16:38.489: E/AndroidRuntime(396):  at android.app.ActivityThread.main(ActivityThread.java:4627)
11-18 00:16:38.489: E/AndroidRuntime(396):  at java.lang.reflect.Method.invokeNative(Native Method)
11-18 00:16:38.489: E/AndroidRuntime(396):  at java.lang.reflect.Method.invoke(Method.java:521)
11-18 00:16:38.489: E/AndroidRuntime(396):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-18 00:16:38.489: E/AndroidRuntime(396):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-18 00:16:38.489: E/AndroidRuntime(396):  at dalvik.system.NativeStart.main(Native Method)
11-18 00:16:38.489: E/AndroidRuntime(396): Caused by: android.database.sqlite.SQLiteException: no such table: translate: , while compiling: SELECT _id, kata_indo, kata_tora from translate ORDER BY _id ASC
11-18 00:16:38.489: E/AndroidRuntime(396):  at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
11-18 00:16:38.489: E/AndroidRuntime(396):  at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91)
11-18 00:16:38.489: E/AndroidRuntime(396):  at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64)
11-18 00:16:38.489: E/AndroidRuntime(396):  at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:80)
11-18 00:16:38.489: E/AndroidRuntime(396):  at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:46)
11-18 00:16:38.489: E/AndroidRuntime(396):  at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42)
11-18 00:16:38.489: E/AndroidRuntime(396):  at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1345)
11-18 00:16:38.489: E/AndroidRuntime(396):  at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1315)
11-18 00:16:38.489: E/AndroidRuntime(396):  at com.han.DbHelper.getAll(DbHelper.java:149)
11-18 00:16:38.489: E/AndroidRuntime(396):  at com.han.Insert.onCreate(Insert.java:31)
11-18 00:16:38.489: E/AndroidRuntime(396):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-18 00:16:38.489: E/AndroidRuntime(396):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

此错误:no such table: translate,当您确定表的名称和请求写得正确时,通常意味着您正在处理空数据库或由于创建而丢失表由于某些原因,这一次失败了。

例如,我确定您对此表的数据库进行了第一次访问吗?如果您选择某些内容,您将看到没有表格或数据。

此错误来自创建数据库时的错误。基本上大多数应用程序正在做的是在设备中创建一个空数据库,然后将应用程序提供的内容复制到空asset文件夹中。但是,如果进程中出现错误,则会取消副本,并且数据库仍为空。如果以编程方式创建所有数据库,则应表示表创建失败。

尝试在数据库上Select *查看是否有内容,然后检查数据库创建过程。您可能会发现缺少某个表,或者db的创建失败。