Android 数据库 SQLiteOpenHelper

时间:2022-09-12 18:48:28

public class DbOpenHelper extends SQLiteOpenHelper {

    private static String name = "test.db"; /* 数据库名称 */
    private static int version = 1; /* 数据库版本号 */

    public DbOpenHelper(Context context) {
        super(context, name, null, version);
        // TODO Auto-generated constructor stub
    }

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

        String sql = "create table person(id integer primary key autoincrement, name varchar(64))";
        db.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub

    }
}
private Button button;

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

        button = (Button) this.findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                DbOpenHelper dbOpenHelper = new DbOpenHelper(MainActivity.this);
                dbOpenHelper.getWritableDatabase();
            }
        });
    }

Android 数据库 SQLiteOpenHelper