android数据库开发

 
 
public class MyDataBaseHelper extends SQLiteOpenHelper {

    public static final String CREATE_BOOK ="create table Book("+
            //primary key 将id列设为主键    autoincrement表示id列是自增长的
"id integer primary key autoincrement,"+
            "author text,"+
            "price real,"+
            "name text)";
    public static final String CREATE_CATEGORY ="create table Category("+
            "id interger primary key autoincrement,"+
            "category_name text,"+
            "category_code integer)";            

    private Context mContext;

    //构造方法:第一个参数Context,第二个参数数据库名,第三个参数cursor允许我们在查询数据的时候返回一个自定义
    //的光标位置,一般传入的都是null,第四个参数表示目前库的版本号
    public MyDataBaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
        mContext = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        //调用SQLiteDatabase中的execSQL()执行建表语句
        db.execSQL(CREATE_BOOK);
        db.execSQL(CREATE_CATEGORY);
        //创建成功
        Toast.makeText(mContext,"created success",Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        //如果Book、Category表已存在则删除表
        db.execSQL("drop table if exists Book");
        db.execSQL("drop table if exists Category");
        onCreate(db);

    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="littlestory.com.sqlitedemo.MainActivity">

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="创建数据库" />
    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="数据增" />
    <Button
        android:id="@+id/button3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="数据删" />
    <Button
        android:id="@+id/button4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="数据改" />
    <Button
        android:id="@+id/button5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="数据查" />
    <TextView
        android:id="@+id/tv_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="shuju"/>
</LinearLayout>
package littlestory.com.sqlitedemo;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private MyDataBaseHelper dbHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        dbHelper=new MyDataBaseHelper(this,"BookStore.db",null,1);
        Button button1= (Button) findViewById(R.id.button1);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dbHelper.getWritableDatabase();
            }
        });
        //数据增加
        Button button2= (Button) findViewById(R.id.button2);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db=dbHelper.getWritableDatabase();
                ContentValues values=new ContentValues();
                values.put("name","The Da Vinci Code");
                values.put("price",20);
                values.put("pages",60);
                values.put("author","Scott");
                db.insert("Book",null,values);
                values.clear();
                values.put("name","The Lost Symbol");
                values.put("author","Scott");
                values.put("pages",500);
                values.put("price",30);
                db.insert("Book",null,values);
            }
        });
        //数据修改
        Button button4= (Button) findViewById(R.id.button4);
        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db=dbHelper.getWritableDatabase();
                ContentValues values=new ContentValues();
                values.put("price",19.9);
                //仔细update中提示的参数(String table,ContentValues,String whereClause,String[] whereArgs)
                //第三滴四行指定具体更新那几行。注意第三个参数中的?是一个占位符,通过第四个参数为第三个参数中占位符指
                // 定相应的内容。
                db.update("Book",values,"name=?",new String[]{"The Da Vinci Code"});

            }
        });
        //删除数据
        //delete(String table, String whereClause, String [] whereArgs)
        //举个例子 delete(“Book”,”name=?”,new String[] {“The Da Vinci Code”});
        //就是把名字为The Da Vinci Code 这本书的数据都删除。
        //再比如:delete(“Book”,”price > ?”,new String[] {“20”});
        //意思就是把价格大于20 的所有的书都删掉。
        //哈哈还是很好玩的,想删什么删什么,但是要注意了 如果你第二,第三个参数不指定的话,就会删除所有行。

        //查询数据
//        query(String table , String [] columns , String selection , String [] selectionArgs , String groupBy , String having , String orderBy );
//        table —指定查询的表名
//        columns —指定查询的列名
//        selection —指定where的约束条件
//        selectionArgs — 为where中的占位符提供具体的值
//        groupBy — 指定需要 groupBy 的列
//        having — 对groupBy后的结果进一步约束
//        orderBy — 指定查询结果的排序方式
        Button button5= (Button) findViewById(R.id.button5);
        TextView tv_text= (TextView) findViewById(R.id.tv_text);
        button5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db=dbHelper.getWritableDatabase();
                //指明去查询Book表
                Cursor cursor=db.query("Book",null,null,null,null,null,null);
                //调用moveToFirst()将数据指针移动到第一行的位置。
                if(cursor.moveToFirst()){
                    do{
                        //然后通过Cursor的getColumnIndex()获取某一列中所对应的位置的索引
                        String name = cursor.getString(cursor.getColumnIndex("name"));
                        String author= cursor.getString(cursor.getColumnIndex("author"));
                        int pages =cursor.getInt(cursor.getColumnIndex("pages"));
                        double price = cursor.getDouble(cursor.getColumnIndex("pricce"));

                    }while (cursor.moveToNext());
                }

                cursor.close();
            }
        });

    }

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325923480&siteId=291194637