Ich entwickle eine Android-Anwendung, die eine SQLite onCreate erstellt und Datensätze abfragt. Alles funktioniert auf meinem Android-Emulator, aber wenn ich auf meinem Android-Gerät (Samsung Galaxy 7 Edge) ausstelle/debugge, scheint die Datenbank leer.SQLite funktioniert nicht auf einem echten Gerät, aber arbeitet auf Emulator
Ich habe es auf meinem Emulator getestet und funktioniert gut. Warum scheint der SQLite nicht auf meinem Telefon erstellt/abgefragt zu werden? Benötige ich zusätzliche Berechtigungen in meinem Manifest?
Bitte helfen. Unten ist mein Code
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();
}
}
}
unten ist mein Manifest:
<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>
Teilen Sie Ihre dbhelper Klasse – Bek
Sie bitte Ihre Frage bearbeiten und eine [MCVE] bieten. Das würde Ihren Code einschließen, der mit SQLite und Ihrem Testfall funktioniert, der veranschaulicht, wie Sie feststellen, dass "die Datenbank leer scheint". – CommonsWare
Welchen API-Level haben Sie auf Ihrem Emulator? – Bek