我正在开发一个Android应用程序,它创建一个SQLite onCreate并查询记录。所有这些都在我的Android模拟器上工作,但当我在我的Android设备(三星Galaxy 7 Edge)上部署/调试时,数据库似乎是空的。
我已在我的模拟器上测试过它并且工作正常。为什么我的手机上似乎没有创建/查询SQLite?我的清单中是否需要其他权限?
请帮忙。以下是我的代码
package com.example.android.cebuanotagalogtranslator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
Button btn_clear;
Button btn_translate_to_ceb;
Button btn_translate_to_fil;
EditText txt_input;
EditText txt_output;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//HABAGAT CONTROLS BEGIN
btn_clear = (Button) findViewById(R.id.btn_clear);
btn_translate_to_ceb = (Button) findViewById(R.id.btn_trans_to_ceb);
btn_translate_to_fil = (Button) findViewById(R.id.btn_trans_to_fil);
txt_input = (EditText) findViewById(R.id.input);
txt_output = (EditText) findViewById(R.id.output);
//HABAGAT : CLEAR BOTH TEXT
btn_clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
txt_input.setText("");
txt_output.setText("");
}
});
//: FILIPINO -> CEBUANO
btn_translate_to_ceb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
String textinput = txt_input.getText().toString();
textinput = "\""+ textinput +"\"";
String filToCebQuery = "SELECT ceb FROM filtoceb WHERE fil = "+ textinput+" "+"COLLATE NOCASE";
SQLiteDatabase DB = openOrCreateDatabase("filtoceb", MODE_PRIVATE, null);
//Cursor c = DB.rawQuery("SELECT * FROM filtoceb", null);
Cursor c = DB.rawQuery(filToCebQuery, null);
if (c!=null)
{
System.out.println("RESULT FOUND : FIL -> CEB");
try {
c.moveToFirst();
while(!c.isAfterLast()){
String result = c.getString(c.getColumnIndex("ceb"));
txt_output.setText(result);
c.moveToNext();
}
}
finally
{
c.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
//: CEBUANO -> FILIPINO
btn_translate_to_fil.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
String textinput = txt_input.getText().toString();
textinput = "\""+ textinput +"\"";
String filToCebQuery = "SELECT fil FROM filtoceb WHERE ceb = "+ textinput+" "+"COLLATE NOCASE";
SQLiteDatabase DB = openOrCreateDatabase("filtoceb", MODE_PRIVATE, null);
Cursor c = DB.rawQuery(filToCebQuery, null);
if (c!=null)
System.out.println("RESULT FOUND : CEB -> FIL ");
{
try {
c.moveToFirst();
while(!c.isAfterLast()){
String result = c.getString(c.getColumnIndex("fil"));
txt_output.setText(result);
c.moveToNext();
}
}
finally
{
c.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
// CREATE DB OPEN IF NOT CREATED YET
try {
SQLiteDatabase eventsDB = this.openOrCreateDatabase("filtoceb", MODE_PRIVATE, null);
//DELETE INITIALIZE SETUP START CLEAN
eventsDB.delete("filtoceb", "1", null);
eventsDB.execSQL("CREATE TABLE IF NOT EXISTS filtoceb (fil VARCHAR, ceb VARCHAR)");
eventsDB.execSQL("INSERT INTO filtoceb (ceb, fil) VALUES ('Kumusta ka?','Kumusta ka?')");
eventsDB.execSQL("INSERT INTO filtoceb (ceb, fil) VALUES ('Maayo, salamat', 'Mabuti naman, salamat')");
eventsDB.execSQL("INSERT INTO filtoceb (ceb, fil) VALUES ('Unsay imong pangalan?', 'Ano pangalan mo?')");
eventsDB.execSQL("INSERT INTO filtoceb (ceb, fil) VALUES ('Unsay ngalan mo?', 'ano pangalan mo?')");
eventsDB.execSQL("INSERT INTO filtoceb (CEB, FIL) values ('Maayo nga nagka-ila ta', 'Ikinagagalak kitang makilala')");
eventsDB.execSQL("INSERT INTO filtoceb (CEB, FIL) values ('Palihug','Please')");
eventsDB.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
下面是我的清单:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
答案 0 :(得分:1)
我认为您的问题可能是由于以下行的位置。
eventsDB.delete("filtoceb", "1", null);
如果数据库不存在,这将导致失败,因为当您执行此行时,tabled将不存在。
解决方案是将线移动到直到创建表格之后。
所以而不是: -
try {
SQLiteDatabase eventsDB = this.openOrCreateDatabase("filtoceb", MODE_PRIVATE, null);
//DELETE INITIALIZE SETUP START CLEAN
eventsDB.delete("filtoceb", "1", null);
eventsDB.execSQL("CREATE TABLE IF NOT EXISTS filtoceb (fil VARCHAR, ceb VARCHAR)");
eventsDB.execSQL("INSERT INTO filtoceb (ceb, fil) VALUES ('Kumusta ka?','Kumusta ka?')");
eventsDB.execSQL("INSERT INTO filtoceb (ceb, fil) VALUES ('Maayo, salamat', 'Mabuti naman, salamat')");
eventsDB.execSQL("INSERT INTO filtoceb (ceb, fil) VALUES ('Unsay imong pangalan?', 'Ano pangalan mo?')");
eventsDB.execSQL("INSERT INTO filtoceb (ceb, fil) VALUES ('Unsay ngalan mo?', 'ano pangalan mo?')");
eventsDB.execSQL("INSERT INTO filtoceb (CEB, FIL) values ('Maayo nga nagka-ila ta', 'Ikinagagalak kitang makilala')");
eventsDB.execSQL("INSERT INTO filtoceb (CEB, FIL) values ('Palihug','Please')");
eventsDB.close();
} catch (Exception e) {
e.printStackTrace();
}
使用: -
try {
SQLiteDatabase eventsDB = this.openOrCreateDatabase("filtoceb", MODE_PRIVATE, null);
eventsDB.execSQL("CREATE TABLE IF NOT EXISTS filtoceb (fil VARCHAR, ceb VARCHAR)");
//DELETE INITIALIZE SETUP START CLEAN
eventsDB.delete("filtoceb", "1", null);
eventsDB.execSQL("INSERT INTO filtoceb (ceb, fil) VALUES ('Kumusta ka?','Kumusta ka?')");
eventsDB.execSQL("INSERT INTO filtoceb (ceb, fil) VALUES ('Maayo, salamat', 'Mabuti naman, salamat')");
eventsDB.execSQL("INSERT INTO filtoceb (ceb, fil) VALUES ('Unsay imong pangalan?', 'Ano pangalan mo?')");
eventsDB.execSQL("INSERT INTO filtoceb (ceb, fil) VALUES ('Unsay ngalan mo?', 'ano pangalan mo?')");
eventsDB.execSQL("INSERT INTO filtoceb (CEB, FIL) values ('Maayo nga nagka-ila ta', 'Ikinagagalak kitang makilala')");
eventsDB.execSQL("INSERT INTO filtoceb (CEB, FIL) values ('Palihug','Please')");
eventsDB.close();
} catch (Exception e) {
e.printStackTrace();
}