0

我的 logcat 中有错误。这里

Caused by: android.database.sqlite.SQLiteException: table japanesebaybayin already exists (code 1): , while compiling: CREATE TABLE japanesebaybayin (_id INTEGER, language_japaneseTEXT, language_japaneseeaTEXT, language_tagalogTEXT, language_definitionTEXT);

有人可以帮我找到正确的解决方案吗?我无法检索数据库中的特定行。

这是我的代码:

public class TranslationActivity extends Activity implements OnClickListener{

protected SQLiteDatabase db;
protected EditText txtenglishAplha;
protected TextView tvdefinition;
protected TextView tvtagalog;
public static final String KEY_ROWID="_id";
public static final String KEY_JAPANESE="language_japanese";
public static final String KEY_JAPANESEEA="language_japaneseea";
public static final String KEY_TAGALOG="language_tagalog";
public static final String KEY_DEFINITION="language_definition";

private final String DATABASE_NAME = "mydb";
private final String DATABASE_TABLE = "japanesebaybayin";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.translation_main);

    db =  this.openOrCreateDatabase(DATABASE_NAME, MODE_PRIVATE, null);

    //db.execSQL("CREATE TABLE IF NOT EXISTS " +
    //      DATABASE_TABLE +
    //      " (rowid INTEGER, japancharacter VARCHAR," +
    //      " japanalphabet VARCHAR, tagalog VARCHAR, " + 
    //      " definition VARCHAR);");

    db.execSQL( "CREATE TABLE " + DATABASE_TABLE + " (" +
            KEY_ROWID + " INTEGER, "+ 
            KEY_JAPANESE + "TEXT, " +
            KEY_JAPANESEEA + "TEXT, " +
            KEY_TAGALOG + "TEXT, " +
            KEY_DEFINITION + "TEXT);"
    );

    db.execSQL("INSERT INTO " +
            DATABASE_TABLE +
            " Values (1,'\u3042\u308A\u304C\u3068\u3046','arigato','salamat','giving thanks');");
    db.execSQL("INSERT INTO " +
            DATABASE_TABLE +
            " Values (2,'\u3042\u308A\u304C\u3068\u3046','sayonara','paalam','giving goodbye');");

     //Buttons
    Button btnsearch = (Button) findViewById(R.id.btnsearch);
    btnsearch.setOnClickListener(this);

    //Edit Texts
    txtenglishAplha = (EditText) findViewById(R.id.txtenglishAlpha);
    txtenglishAplha.requestFocus();

    //Text Views
    tvdefinition = (TextView) findViewById(R.id.tvdefinition);
    tvtagalog = (TextView) findViewById(R.id.tvtagalog);


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.translation, menu);
    return true;
}

@Override
public void onClick(View view) {
    // TODO Auto-generated method stub
    String tagalog="", definition="", japanalpha;
    Cursor cursor;
    switch (view.getId()){
    case R.id.btnsearch:

         japanalpha = txtenglishAplha.getText().toString();
         String[] columns = new String[] {KEY_JAPANESEEA, KEY_TAGALOG, KEY_DEFINITION};
            cursor = db.query(DATABASE_TABLE, columns, KEY_JAPANESEEA + "='"+japanalpha+"'", null, null, null, null);

         //String qjoiner = "SELECT japanalphabet, tagalog, definition from " +DATABASE_TABLE+ " where japanalphabet ='"+japanalpha+"';";
         //Log.i("-----search table joiner----- ",qjoiner);
        // cursor=db.rawQuery(qjoiner, null);
         if(cursor != null){
             cursor.moveToFirst();
             tagalog = cursor.getString(cursor.getColumnIndex(KEY_TAGALOG)).toString();
             definition = cursor.getString(cursor.getColumnIndex(KEY_DEFINITION)).toString();
             tvtagalog.setText(tagalog);
             tvdefinition.setText(definition);
         }else{
                 tvtagalog.setText(tagalog);
                 tvdefinition.setText(definition);
         }

    break;
    }

}

}
4

4 回答 4

1

Your table creation query is wrong. See the below image. I am working to get the solution.

enter image description here

here is the code which you want.

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;


public class Test extends Activity {


protected SQLiteDatabase db;
public static final String KEY_ROWID="_id";
public static final String KEY_JAPANESE="language_japanese";
public static final String KEY_JAPANESEEA="language_japaneseea";
public static final String KEY_TAGALOG="language_tagalog";
public static final String KEY_DEFINITION="language_definition";
private final String DATABASE_NAME = "mydb";
private final String DATABASE_TABLE = "japanesebaybayin";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        db =  this.openOrCreateDatabase(DATABASE_NAME, MODE_PRIVATE, null);    
        db.execSQL( "CREATE TABLE if not exists " + DATABASE_TABLE + " (" +
                KEY_ROWID + "  INTEGER, "+ 
                KEY_JAPANESE + " TEXT, " +
                KEY_JAPANESEEA + " TEXT, " +
                KEY_TAGALOG + "  TEXT, " + 
                KEY_DEFINITION + " TEXT);" // the wrong which you made is here there was no space between column name and type. 
        );

        db.execSQL("INSERT INTO " +
                DATABASE_TABLE +
                " Values (1,'\u3042\u308A\u304C\u3068\u3046','arigato','salamat','giving thanks');");
        db.execSQL("INSERT INTO " +
                DATABASE_TABLE +
                " Values (2,'\u3042\u308A\u304C\u3068\u3046','sayonara','paalam','giving goodbye');");





        Cursor mCursor = null;

        try {
            mCursor = db.rawQuery(" SELECT * FROM japanesebaybayin where language_japaneseea='arigato'", null);
            // you can change it above query with dynamic inputs. 


        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }

        if(mCursor != null){
            mCursor.moveToFirst();

            mCursor.getString(mCursor.getColumnIndex(KEY_DEFINITION)).toString();

             System.out.println(" 1111 " + mCursor.getString(mCursor.getColumnIndex(KEY_TAGALOG)).toString());
             System.out.println(" 222 " + mCursor.getString(mCursor.getColumnIndex(KEY_JAPANESEEA)).toString());
             System.out.println(" 333 " + mCursor.getString(mCursor.getColumnIndex(KEY_JAPANESE)).toString());
             System.out.println(" 444 " + mCursor.getString(mCursor.getColumnIndex(KEY_DEFINITION)).toString());


        }  


}


}
于 2013-06-06T14:48:16.073 回答
0

导致您的错误是因为每次启动应用程序时,您都试图在数据库中创建表。但是使用数据库的全部意义在于存储数据(无论您的应用程序是否正在运行),因此该表已经存在。

一个快速的解决方法是只执行“如果不存在则创建表”,但更好的方法是研究如何在 Android 中使用 SQLite dbs,特别是 OpenHelper 类。http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html

于 2013-06-06T14:27:06.303 回答
0

哎呀!所有那些管道代码都不是很好。我知道 SQLite 是必需的,但您可以尝试 ORMLite,它是 SQLite 的“休眠”实现。

检查我的示例,使用所有 crud 操作以及更多具有标准的方法:

https://github.com/nicolasjafelle/AndroidOrmLiteDemo

正如我所说,这只是一个示例应用程序,但您可以复制和粘贴并使用它,风险自负 =D。

于 2013-06-06T14:34:18.523 回答
0

每次活动/片段开始时,您都在尝试创建数据库表。

你应该只创建一次,否则你会得到你得到的错误,表明表已经存在。

您应该阅读SqliteOpenHelper的文档并使用它,因为它将您的数据库代码与您的活动代码分开,并允许您干净地处理数据库更新等。

于 2013-06-06T14:24:21.897 回答