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

Android 数据库 SQLiteOpenHelper

上一篇:看完不会的来打我!Java学习路线指南,妈妈再也不用担心我找工作了!


下一篇:javascript-Sinonjs如何存根依赖注入类?