Android 数据库 SQLiteOpenHelper [复制链接]

2019-12-5 13:57
byebye0521 阅读:158 评论:0 赞:0
Tag:  Android sql
复制代码
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();
            }
        });
    }
复制代码


我来说两句
您需要登录后才可以评论 登录 | 立即注册
facelist
所有评论(0)
领先的中文移动开发者社区
18620764416
7*24全天服务
意见反馈:1294855032@qq.com

扫一扫关注我们

Powered by Discuz! X3.2© 2001-2019 Comsenz Inc.( 粤ICP备15117877号 )