Android之SQLite

在模拟器运行的情况下,进入cmd运行adb shell 可进入模拟器的linux系统
输入 lite3 mars_test_db 可进入sqlite模式
.schema或者.sch 查看有哪些表

 

SQLite帮助类

package com.example.mars_1400_sqlite01;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

//DatabaseHelper作为一个访问SQLite的助手类,提供两个方面的功能
//第一:getReadableDatabase(),getWritableDatabase()可以获得SQLiteDatabase对象,通过该对象可以对数据库进行操作
//第二:提供了onCreate()和onUpgrade()两个回调函数,允许我们在创建和升级数据库时,进行自己的操作
public class DatabaseHelper extends SQLiteOpenHelper {

    private static final int VERSION = 1;
    //在SQLiteOpenHelper子类中,必须有该构造函数
    public DatabaseHelper(Context context, String name, CursorFactory factory,
            int version) {
        // 必须通过super调用父类当中的构造函数
        super(context, name, factory, version);
    }

    public DatabaseHelper(Context context, String name) {
        this(context, name, VERSION);
    }

    public DatabaseHelper(Context context, String name, int version) {
        this(context, name, null, version);
    }

    //该函数是在第一次创建数据库的时候执行,实际上是在第一次得到SQLiteDatabase对象的时候
    @Override
    public void onCreate(SQLiteDatabase db) {
        System.out.println("create a Database");
        //execSQL函数用于执行sql语句
        db.execSQL("create table user(id int,name varchar(20))");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        //当数据库的版本更新时执行
        System.out.println("update a Database");
    }

}

 

SQLite增删改查

public class MainActivity extends Activity {
    private Button createDatabase = null;
    private Button updateDatabase = null;
    private Button insert = null;
    private Button update = null;
    private Button query = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        createDatabase = (Button) findViewById(R.id.createDatabase);
        updateDatabase = (Button) findViewById(R.id.updateDatabase);
        insert = (Button) findViewById(R.id.insert);
        update = (Button) findViewById(R.id.update);
        query = (Button) findViewById(R.id.query);

        createDatabase.setOnClickListener(new CreateListener());
        updateDatabase.setOnClickListener(new UpdateListener());
        insert.setOnClickListener(new InsertListener());
        update.setOnClickListener(new UpdateRecordListener());
        query.setOnClickListener(new QueryListener());
        
        /*
         * if (savedInstanceState == null) {
         * getSupportFragmentManager().beginTransaction() .add(R.id.container,
         * new PlaceholderFragment()).commit(); }
         */
    }

    class CreateListener implements OnClickListener {
        @Override
        public void onClick(View v) {
            // 创建一个DatabaseHelper对象
            DatabaseHelper dbHelper = new DatabaseHelper(MainActivity.this,
                    "mars_test_db");
            // 只有调用了DatabaseHelper对象的getReadableDatabase()方法,或者是getWritableDatabase()方法之一
            // 才会创建数据库
            SQLiteDatabase db = dbHelper.getReadableDatabase();
        }
    }

    class UpdateListener implements OnClickListener {
        @Override
        public void onClick(View v) {
            // 更新数据库的版本
            DatabaseHelper dbHelper = new DatabaseHelper(MainActivity.this,
                    "mars_test_db", 2);
            SQLiteDatabase db = dbHelper.getReadableDatabase();
        }
    }

    // 更新操作就相当于执行SQL语句当中的update语句
    // update table_name set xxcol=xx where xxclo=xx...
    class InsertListener implements OnClickListener {
        @Override
        public void onClick(View v) {
            System.out.println("insert data");
            // 生成ContentValues对象
            ContentValues values = new ContentValues();
            // 想该对象当中插入键值对,其中键是列名,值是希望插入到这一列的值,值必须和数据类型匹配
            values.put("id", 2);
            values.put("name", "zhangsan");
            DatabaseHelper dbHelper = new DatabaseHelper(MainActivity.this,
                    "mars_test_db");
            SQLiteDatabase db = dbHelper.getWritableDatabase();
            // 调用insert方法,就可以将数据插入到数据库当中
            db.insert("user", null, values);
        }
    }

    class UpdateRecordListener implements OnClickListener {
        @Override
        public void onClick(View v) {
            DatabaseHelper dbHelper = new DatabaseHelper(MainActivity.this,
                    "mars_test_db");
            SQLiteDatabase db = dbHelper.getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put("name", "wzh");
            db.update("user", values, "id=?", new String[] { "1" });
        }
    }

    class QueryListener implements OnClickListener {
        @Override
        public void onClick(View v) {
            DatabaseHelper dbHelper = new DatabaseHelper(MainActivity.this,
                    "mars_test_db");
            SQLiteDatabase db = dbHelper.getReadableDatabase();
            Cursor cursor = db.query("user", new String[] { "id", "name" },
                    "id=?", new String[] { "1" }, null, null, null);
            while (cursor.moveToNext()) {
                String name = cursor.getString(cursor.getColumnIndex("name"));
                System.out.println("query--->" + name);
            }
        }
    }

 

你可能感兴趣的:(android)