简体   繁体   English

数据库在模拟器中工作正常,但在设备上出错

[英]Database works fine in emulator, but gives an error on device

My app creates a database the moment it runs, and stores some basic data in it. 我的应用程序在运行时创建数据库,并在其中存储一些基本数据。 However, when I try to run the app on the device, I am getting "Unable to open database" error, while it is working and is fully functional on the emulator. 但是,当我尝试在设备上运行应用程序时,我收到“无法打开数据库”错误,但它正在运行并且在模拟器上完全正常运行。

my main activity is 我的主要活动是

import android.app.Activity;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.view.Menu;
import android.widget.ListView;

public class MainActivity extends Activity
{

@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Log.i("sarhad","Initializing data");
    DatabaseHelper db = new DatabaseHelper(this);
    Cursor cursor;
    ListView listSongs;
    SimpleCursorAdapter adapter;
    final String[] FROM = { db.KEY_SONGNAME, db.KEY_ARTISTNAME, db.KEY_LABELNAME };
    final int[] TO = { R.id.textViewSONG, R.id.textViewARTIST, R.id.textViewLABEL };    

    try
    {
        db.openDataBase();

    } catch (SQLException sqle)
    {
        throw sqle;
    }


    listSongs = (ListView) findViewById(R.id.listSongsView);

    db.getWritableDatabase();

    Log.i("tag","Initializing query");
    String queryStr = "SELECT * FROM songs";

    Log.i("tag","before : cursor = db.getSongs(queryStr);");
    cursor = db.getSongs(queryStr);
    Log.i("tag","after : cursor = db.getSongs(queryStr);");

     startManagingCursor(cursor);

     adapter = new SimpleCursorAdapter(this, R.layout.row, cursor, FROM,TO);
     listSongs.setAdapter(adapter);
}

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

} }

and my databasehelper class is 我的databasehelper类是

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper
{
static final int DATABASE_VERSION = 1;

public static String DB_PATH = "";

// Database Name
static final String DATABASE_NAME = "songsDB";

// Contacts table name
static final String TABLE_SONGS = "songs";

// Contacts Table Columns names
static final String KEY_ID = "_id";
static final String KEY_SONGNAME = "songname";
static final String KEY_ARTISTNAME = "artistname";
static final String KEY_LABELNAME = "labelname";

private SQLiteDatabase db = null;
private final Context myContext = null;

private static DatabaseHelper mDBConnection;

//////////////////////////////////////////////////////////////////////////////////

public DatabaseHelper(Context context)
{
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    DB_PATH = "/data/data/" + context.getApplicationContext().getPackageName() + "/databases/";
}

//////////////////////////////////////////////////////////////////////////////////

// Creating Tables
@Override
public void onCreate(SQLiteDatabase db)
{
    String CREATE_SONGS_TABLE = "CREATE TABLE " + TABLE_SONGS + "("
            + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_SONGNAME
            + " TEXT," + KEY_ARTISTNAME + " TEXT," + KEY_LABELNAME
            + " TEXT);";
    db.execSQL(CREATE_SONGS_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_SONGS);
    onCreate(db);
}

//////////////////////////////////////////////////////////////////////////////////


void addSong(String song, String singer, String label)
{
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_SONGNAME, song);
    values.put(KEY_ARTISTNAME, singer);
    values.put(KEY_LABELNAME, label);

    db.insert(TABLE_SONGS, null, values);
    db.close(); 
}

public Cursor getSongs(String queryStr)
{
    return db.rawQuery(queryStr, null);

}

///////////////////////////////////////////////////////////////////////

public SQLiteDatabase openDataBase() throws SQLException
{
    String myPath = DB_PATH + DATABASE_NAME;
    db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
    return db;
}

/**
 * Close the database if exist
 */
@Override
public synchronized void close()
{
    if (db != null)
        db.close();
    super.close();
}

} }

I think you messed up the path and the real device won't let you put a file in that location, while the emulator will. 我认为你弄乱了路径,真正的设备不会让你把文件放在那个位置,而模拟器会。 Try removing the path entirely and letting the system figure out where to put the database. 尝试完全删除路径,让系统找出放置数据库的位置。 Once you know it works like that, then you can go back and modify the path if you need to. 一旦你知道它就像那样工作,那么你可以回去修改路径,如果你需要的话。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android数据库应用程序在模拟器上运行良好,但在设备上显示错误 - Android database application runs fine on emulator but gives an error on the device Facebook OAuth在设备上显示错误,但可在模拟器上运行 - Facebook OAuth Gives Error on Device but Works on Emulator Device Admin API示例:在设备上的模拟器NoSuchMethod错误上可以正常工作 - Device Admin API sample: works fine on emulator NoSuchMethod error on Device SQLite数据库Android应用程序在模拟器中无法在我的设备中正常运行 - SQLite database Android app works fine in emulator not in my device SVG文件在模拟器中运行正常,但在Real Device中出错? - SVG file run fine in Emulator but Gives error in Real Device? Webview在模拟器上工作正常,但在设备上工作不正常 - Webview works fine on emulator but not on device 在设备上运行时没有这样的表错误,但在模拟器上相同的代码工作正常 - No such table error when running on device but on emulator same code works fine 检测设备是否已植根或无法在设备上正常运行,但不能在模拟器上运行 - Detecting device is rooted or not works fine on device but not on emulator 应用程序在模拟器上运行良好,但在设备上显示NoClassDefFoundError - Application runs fine on emulator but gives NoClassDefFoundError on device 应用程序可以在模拟器上正常运行,但不能在移动设备上运行 - App works fine on Emulator but not on Mobile device
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM