在模拟器上一切正常,在实际转移时,代码无效

时间:2017-03-09 09:15:05

标签: android sqlite android-sqlite

遇到问题,我正在浏览SQL数据库课程。在模拟器上一切正常,日志显示表中有一个条目,在真实设备上 - 日志,没有。设备 - 魅族M3 Note。

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

2 个答案:

答案 0 :(得分:1)

  

03-08 12:38:56.506 5161-5161 / com.example.opimand.simplesqlite   E / System:stat文件错误,路径是   /data/app/com.example.opimand.simplesqlite-2/lib/arm64,异常是   android.system.ErrnoException:stat failed:ENOENT(没有这样的文件或   目录)

我认为你的问题的关键在... / arm64 你的应用程序如果试图寻找64位二进制文​​件,它可能在模拟器中找到,但不在你的设备中

编辑 - 您的设备是魅族M3 采用联发科技MT6750处理器,配备8个ARM Cortex-A53,64位CPU内核和Mali-T860显卡。所以这不应该是你的问题。

https://liliputing.com/2016/04/meizu-m3-is-an-octa-core-64-bit-smartphone-for-92.html

您可以在此处找到有关此内容的更多信息:

https://github.com/realm/realm-java/issues/705

答案 1 :(得分:0)

您错误地定义了数据库路径。您的数据库将驻留在应用程序包目录中的数据库文件夹中。

应该是

/data/data/com.example.opimand.simplesqlite-2/databases/