什么时候SQLite数据库初始化?

时间:2013-10-01 06:33:21

标签: java android sqlite

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

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 个答案:

答案 0 :(得分:1)

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

SQLiteDatabase sqliteDatabase = null;

sql1 databaseHelper = new sql1(_context);

sqliteDatabase = databaseHelper.getWritableDatabase();

答案 1 :(得分:0)

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

答案 2 :(得分: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

    }

    }

答案 3 :(得分:0)

在第一次查询数据库之前,不会调用onCreate SQLiteOpenHelper调用,更具体地说,第一次调用getWritableDatabasegetReadableDatabase时。{ / p>