欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 数据库 SQLiteOpenHelper

程序员文章站 2022-07-02 12:46:29
public class DbOpenHelper extends SQLiteOpenHelper { private static String name = "test.db"; /* 数据库名称 */ private static int version = 1; /* 数据库版本号 */ ... ......
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();
            }
        });
    }