android 之 SQLite

创建MyDatabaseHelper继承自SQLiteOpenHelper:

 public class MyDatabaseHelper extends SQLiteOpenHelper {
//建表
    public static final String CREATE_BOOK = "create table Book("
    +"id integer primary key autoincrement,"
    +"author text,"
    +"price real,"
    +"pages integer,"
    +"name text)";

//新增表
    public static final String CREATE_CATEGORY = "create table Category("
    +" id integer primary key autoincrement,"
    +" category_name text,"
    +" category_code integer,"
    +" category_height integer)";

    private Context mContext;

    public MyDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);

        mContext = context;
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {

        sqLiteDatabase.execSQL(CREATE_BOOK);
        sqLiteDatabase.execSQL(CREATE_CATEGORY);
        Toast.makeText(mContext,"Create succeed",Toast.LENGTH_SHORT).show();

    }

//更新数据库版本
    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

        sqLiteDatabase.execSQL("drop table if exists Book");
        sqLiteDatabase.execSQL("drop table if exists Categoty");
    }
}

执行CRUD操作活动页面:

public class FirstActivity extends BaseActivity {
private MyDatabaseHelper database;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        Log.d("FirstActivity", "Task id is" + getTaskId());
        setContentView(R.layout.first_layout);
        database = new MyDatabaseHelper(this, "BookS.db", null, 2);

//建表
        Button button = (Button) findViewById(R.id.Create_database);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                database.getWritableDatabase();
            }
        });

//增加数据
        Button addData = (Button) findViewById(R.id.add_data);
        addData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                SQLiteDatabase db = database.getWritableDatabase();
                ContentValues values = new ContentValues();

                values.put("name", "The dA BAOBO");
                values.put("author", "hanahm");
                values.put("pages", 123423);
                values.put("price", 12.90);
                db.insert("Book", null, values);

            }
        });
//更新数据
        Button updateButton = (Button) findViewById(R.id.update_data);
        updateButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SQLiteDatabase db = database.getWritableDatabase();
                ContentValues values = new ContentValues();
                values.put("price", 10.99);
                db.update("Book", values, "name = ?", new String[]{"The dA BAOBO"});
            }
        });
//删除表中数据
        Button deleteButton = (Button) findViewById(R.id.delete_data);
        deleteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SQLiteDatabase db = database.getWritableDatabase();
                db.delete("Book", "pages > ?", new String[]{"500"});
            }
        });
//查询表中数据
        Button queryButton = (Button) findViewById(R.id.query_data);
        queryButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SQLiteDatabase db = database.getWritableDatabase();
                //查询表中所有的数据
                Cursor cursor = db.query("Book", null, null, null, null, null, null);
                if (cursor.moveToFirst()) {
                    do {
                        //遍历cursor对象,取出数据并打印
                        String name = cursor.getString(cursor.getColumnIndex("name"));
                        String cuthor = cursor.getString(cursor.getColumnIndex("author"));
                        int pages = cursor.getInt(cursor.getColumnIndex("pages"));
                        double price = cursor.getDouble(cursor.getColumnIndex("price"));
                        Log.d("FirstActivity", "book name is " + name);
                        Log.d("FirstActivity", "book author is " + cuthor);
                        Log.d("FirstActivity", "book pages is " + pages);
                        Log.d("FirstActivity", "book price is " + price);
                    }while (cursor.moveToNext());
                }
                cursor.close();
            }
        });
        }

}}

猜你喜欢

转载自blog.csdn.net/shiios/article/details/76684603
今日推荐