-1

我正在玩一些 SQLLite 基础知识,我尝试从我的 Activity 类中创建一个非常简单的数据库。应用程序运行正常,但是当我进入模拟器的文件资源管理器时,没有创建数据库,这里有什么问题?

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}



class sql1 extends SQLiteOpenHelper {

    public static final String DBNAME="coordinates";
    public static final String TBNAME="show";
    private static final int Version=1;


    public sql1(Context context, String name, CursorFactory factory, int version){
        super(context, DBNAME, factory, Version);
    }

    @Override
    public void onCreate(SQLiteDatabase arg0) {

        String CREATE_TABLE ="CREATE TABLE IF NOT EXISTS Names (Time VARCHAR,Long VARCHAR, Lang VARCHAR);";             

        arg0.execSQL(CREATE_TABLE);
        Log.d("TAG","Created Database");
    }

    @Override
    public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
        // TODO Auto-generated method stub

    }


}


}
4

4 回答 4

1

在您的主要活动中,执行以下操作:

SQLiteDatabase sqliteDatabase = null;

sql1 databaseHelper = new sql1(_context);

sqliteDatabase = databaseHelper.getWritableDatabase();

于 2013-10-01T07:15:45.957 回答
0

我认为问题可能是您使用了 Long 等关键字。分配另一个名称。对于时间,我不确定,但它也可以是关键字

于 2013-10-01T06:37:21.887 回答
0

尝试这个

            public class DbHelper extends SQLiteOpenHelper {

    public DbHelper() {

        super(dataContext, DB_NAME, null, 1);

        DB_PATH = "/data/data/" 
                   + dataContext.getApplicationContext().
                    getPackageName()+ 
                    "/databases/";

        boolean dbExist = checkDataBase();
        if (!dbExist) {

            this.getReadableDatabase();
            try {
                copyDataBase();

            } catch (IOException e) {
                throw new Error("Error copying database");
            }
        }

    }

    private void copyDataBase() throws IOException {
        // TODO Auto-generated method stub

        InputStream inFile = dataContext.getAssets().open(DB_NAME);
        String outFileName = DB_PATH + DB_NAME;
        OutputStream myOutput = new FileOutputStream(outFileName);
        byte[] buffer = new byte[1024];
        int length;
        while ((length = inFile.read(buffer)) > 0) {
            myOutput.write(buffer, 0, length);
        }
        // Close the streams
        myOutput.flush();
        myOutput.close();
        inFile.close();
      }

      private boolean checkDataBase() {
        // TODO Auto-generated method stub

        File dbFile = new File(DB_PATH + DB_NAME);

        return dbFile.exists();



    }

    @Override
    public void onCreate(SQLiteDatabase arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
        // TODO Auto-generated method stub

    }

    }
于 2013-10-01T06:40:04.370 回答
0

直到第一次查询您的数据库时才会onCreate调用 of ,更具体地说是第一次或在其上调用。SQLiteOpenHelpergetWritableDatabasegetReadableDatabase

于 2013-10-01T06:47:00.757 回答