学习一下Android中的重要的部分,那就是对数据库的操作,SQList的操作
MyOpenHelper继承SQListOpenHelper,实现自己的构造方法、创建数据库就调用的onCreat()方法,onUpgrade()方法。(即:创建方法,更新方法)。
package com.example.xm.sqlistdemo1;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class MyOpenHelper extends SQLiteOpenHelper{
//构造方法
/*
* @param context to use to open or create the database
* @param name of the database file, or null for an in-memory database,数据库文件的名字
* @param factory to use for creating cursor objects, or null for the default,游标工厂,传null就用改默认的
* @param version number of the database (starting at 1); if the database is older,版本号,必须从1开始
*/
public MyOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
//数据库创建的时,此方法就会调用
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table person(_id integer primary key autoincrement, name char(10), salary char(20),phone integer(20))");
}
//数据库升级时就调用
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
重写MyOpenHelper方法,传入4个参数:1,上下文,2,数据库文件的名字,3,游标工厂,传入null,就使用默认的游标工厂,4,版本号,从1开始。。
使用getWritableDatebase();获取到数据库对象db,然后对数据库进行操作,
package com.example.xm.sqlistdemo1;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View view) {
MyOpenHelper ch = new MyOpenHelper(this,"people.db",null,1);
//如果数据库不存在就先创建数据库,再获取可读可写的数据库对象,如果数据库存在,就直接打开
SQLiteDatabase db = ch.getWritableDatabase();
//如果存储空间满了,那么返回的是只读数据库
//SQLiteDatabase db = ch.getReadableDatabase();
//给数据库插入数据,使用SQL语句,注意使用占位符,,,
// db.execSQL("insert into person (name, salary, phone) values(?,?,?)",new Object[]{"小明","11111",121212});
// db.execSQL("insert into person (name, salary, phone) values(?,?,?)",new Object[]{"小明11","11111",121212});
//删除数据
//db.execSQL("delete from person where name =?",new Object[]{"小明11"});
//修改数据
//db.execSQL("update person set phone = ? where name = ?", new Object[]{158158158,"小明"});
/*查询数据:
//rawQuery:查到的是结果集,两个参数:1,SQL语句,2,占位符(要是用where的时候就用占位符)
Cursor cursor = db.rawQuery("select name, salary from person", null);
while (cursor.moveToNext()){
//cursor.getColumnIndex:通过索引获取值
String name = cursor.getString(cursor.getColumnIndex("name"));
String salary = cursor.getString(cursor.getColumnIndex("salary"));
System.out.println(name+";"+salary);
}
*/
/*
//使用Api对数据进行插入,
//把要插入的数据全部封装到ContentValues对象中
ContentValues values = new ContentValues();
values.put("name","小明");
values.put("phone","155555");
values.put("salary","1600000");
db.insert("person",null,values);
*/
/*
//使用Api删除数据
//3个参数,:
//1,表名, 2,where条件,3,填充where占位符的数组
//这里他是有返回值的,返回值是1,表示影响了1行,
int i = db.delete("person", "name = ? and _id = ?", new String[]{"小小明","5"});
System.out.println(i);
Log.i("fdfdf","jfdkfjdk");
*/
/*
//修改数据
ContentValues values = new ContentValues();
values.put("salary","1");
db.update("person",values,"_id = ?",new String[]{"6"});
*/
/*
//查询数据
Cursor cursor= db.query("person",null,null,null,null,null,null);
while(cursor.moveToNext()){
String name = cursor.getString(cursor.getColumnIndex("name"));
String phone = cursor.getString(cursor.getColumnIndex("phone"));
String salary = cursor.getString(cursor.getColumnIndex("salary"));
System.out.println(name+"-"+salary+"-"+phone);
}
*/
//设置事务
db.beginTransaction();
ContentValues values = new ContentValues();
values.put("salary", 0);
db.update("person", values, "name = ?", new String[]{"小明明"});
values.clear();
values.put("salary", "1600001");
db.update("person",values,"name = ?",new String[]{"小明"});
//设置 事务执行成功
db.setTransactionSuccessful();
db.endTransaction();
}
}