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

时间:2014-04-09 12:04:03

标签: android sql database sqlite

我是Android编程的新手,所以请帮助我。我使用SQLite浏览器创建了数据库,我尝试将我的应用程序与此数据库文件连接,但它无法正常工作,错误日志说“没有这样的表:datasaya”。如果在我创建的数据库中只有一个表,这怎么可能发生呢。

这是我的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; 

} 

}

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:3)

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