面對一個問題,我正在通過SQL數據庫課。在模擬器上一切正常,日誌顯示錶中有一個條目,在真實設備上 - 日誌,不。設備 - 魅族M3筆記。從仿真器在模擬器上一切正常,在真正的潛水,代碼不工作
package com.example.opimand.simplesqlite;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
final String LOG_TAG = "myLogs";
Button btnAdd, btnRead, btnClear;
EditText etName, etEmail;
DBHelper dBhelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnAdd = (Button) findViewById(R.id.btnAdd);
btnAdd.setOnClickListener(this);
btnRead = (Button) findViewById(R.id.btnRead);
btnRead.setOnClickListener(this);
btnClear = (Button) findViewById(R.id.btnClear);
btnClear.setOnClickListener(this);
etName = (EditText) findViewById(R.id.etName);
etEmail = (EditText) findViewById(R.id.etEmail);
dBhelper = new DBHelper(this);
}
@Override
public void onClick(View v) {
ContentValues cv = new ContentValues();
String name = etName.getText().toString();
String email = etEmail.getText().toString();
SQLiteDatabase db = dBhelper.getWritableDatabase();
switch (v.getId()){
case R.id.btnAdd:
Log.d(LOG_TAG, "---Insert in my table---");
cv.put("name", name);
cv.put("email", email);
long rowId = db.insert("mytable", null, cv);
Log.d(LOG_TAG, "raw inserted, ID "+rowId);
break;
case R.id.btnRead:
Log.d(LOG_TAG, "---Raws in my table ---");
Cursor c = db.query("mytable",null,null,null,null,null,null);
if (c.moveToFirst()){
int idColIndex = c.getColumnIndex("id");
int nameColIndex = c.getColumnIndex("name");
int emailColIndex = c.getColumnIndex("email");
do {
Log.d(LOG_TAG,
"ID = "+c.getInt(idColIndex)+
", name = "+c.getString(nameColIndex)+
", email = "+c.getString(emailColIndex));
} while (c.moveToNext());
}else {
Log.d(LOG_TAG, "0 raws");
c.close();
break;
}
case R.id.btnClear:
Log.d(LOG_TAG, "---Clear my table---");
int clearCount = db.delete("mytable", null, null);
Log.d(LOG_TAG, "deleted raws count ="+clearCount);
break;
}
dBhelper.close();
}
class DBHelper extends SQLiteOpenHelper{
public DBHelper(Context context) {
super(context, "nyDb", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.d(LOG_TAG, "---onCreateDatabase---");
db.execSQL("create table mytable ("
+ "id integer primary key autoincrement,"
+ "name text,"
+ "email text"+");");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
}
日誌 ---的onCreate數據庫--- --- 中插入mytable的:--- 行插入,ID = 1
而從真實設備顯示這些日誌錯誤
03-08 12:38:56.506 5161-5161/com.example.opimand.simplesqlite E/System: stat file error, path is /data/app/com.example.opimand.simplesqlite-2/lib/arm64, exception is android.system.ErrnoException: stat failed: ENOENT (No such file or directory)
03-08 12:38:57.448 5161-5215/com.example.opimand.simplesqlite E/GED: Failed to get GED Log Buf, err(0)
[ 03-08 12:38:57.448 5161: 5215 I/ ]
elapse(include ctx switch):3792 (ms), eglInitialize
您使用一些外部(第三方)的本地庫? – Yazan
不,我使用的所有東西都在代碼中可見 – Opimand