连接与SQLite数据库Android应用程序时无法找到表名

连接与SQLite数据库Android应用程序时无法找到表名

本文介绍了连接与SQLite数据库Android应用程序时无法找到表名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新至Android编程,请帮助我。我已经使用SQLite浏览器创建的数据库中,我尝试连接我的应用程序使用该数据库文件,但它不工作,错误日志中说,没有这样的表:datasaya。怎么会发生这种事,而在我创建的数据库,只有1台。

I am new to android programming so please help me. I have created database using SQLite browser, I am try to connecting my app with this database file, but it is not working, the error log say that "no such table : datasaya". How could this happen while in the database I created there is only 1 table.

这是我的sqlite的辅助类:

public class SQLiteHelper extends SQLiteOpenHelper {


private static String DB_PATH="/data/data/com.example.latihandbsqlite/databases/";
private static String DB_NAME = "cekdatabase.sqlite";
private static int VERSION =1;
private SQLiteDatabase myDataBase;
private final Context myContext;


public SQLiteHelper(Context context) {

    super(context, DB_NAME, null, VERSION);
    myContext = context;

    try {

        createDatabase();
        } catch (IOException ioe) {

            throw new Error("Unable to create database");
            }
    }

public void createDatabase() throws IOException {

    boolean dbExist = checkDataBase();

    if (dbExist) {
        System.out.println("DB EXIST");
        } else {
            this.getReadableDatabase();
            this.close();
            copyDataBase();
            }
    }


private void copyDataBase() throws IOException {

    InputStream myInput = myContext.getAssets().open(DB_NAME);

    String outFileName = DB_PATH + DB_NAME;
    OutputStream myOutput = new FileOutputStream(outFileName);

    byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer)) > 0) {

        myOutput.write(buffer, 0, length);
        }

    myOutput.flush();
    myOutput.close();
    myInput.close();
    }

private boolean checkDataBase() {

    SQLiteDatabase checkDB = null;

    try {

        String myPath = DB_PATH + DB_NAME;
        checkDB = SQLiteDatabase.openDatabase(myPath,
                null, SQLiteDatabase.OPEN_READONLY);
        } catch (SQLiteException e) {

            System.out.println("Database does't exist yet.");
            }

    if (checkDB != null) {

        checkDB.close(); }

    return checkDB != null ? true : false;
    }

@Override

    public synchronized void close() {

        if (myDataBase != null) myDataBase.close();

        super.close();

    } @Override

    public void onCreate(SQLiteDatabase arg0) {


    } @Override

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {


    }

            }

这是我的SQLiteConnector类:

     public class SQLiteConnector {

private SQLiteDatabase database;
private SQLiteHelper sqlHelper;
private Cursor cursor;

private static final String TABLE_RECORD = "datasaya";

public SQLiteConnector(Context context) {

    sqlHelper = new SQLiteHelper(context);

} // Getting All records


public List<String> getAllRecord() {

    List<String> namagambar = new ArrayList<String>();
    String selectQuery = "SELECT * FROM " + TABLE_RECORD;

    database = sqlHelper.getReadableDatabase();
    cursor = database.rawQuery(selectQuery, null);

    if (cursor.moveToFirst()) {

        do {

            namagambar.add(cursor.getString(1));
            } while (cursor.moveToNext());

    }
    database.close();
    return namagambar;

}

}

我怎样才能解决这个问题呢?

How can i resolve this problem ?

推荐答案

删除你的数据库的名称的点。
发件人: cekdatabase.sqlite 以: cekdatabase

Remove the point of the name of your database.From: cekdatabase.sqlite to: cekdatabase.

这篇关于连接与SQLite数据库Android应用程序时无法找到表名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 19:20