Android 数据库 SQLiteOpenHelper

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();
            }
        });
    }

image

posted @ 2019-11-15 19:54  thomas_blog  阅读(395)  评论(0编辑  收藏  举报