设为首页 加入收藏

TOP

Android SQLite API的使用(非原创)(一)
2017-10-11 17:06:18 】 浏览:6489
Tags:Android SQLite API 使用 原创

1.使用SQLite的API来进行数据库的添加、删除、修改、查询

package com.example.sqlitedatabase.test;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.test.AndroidTestCase;

import com.example.sqlitedatabase.MyOpenHelper;

public class JunitTest2 extends AndroidTestCase{

	private MyOpenHelper helper;
	private SQLiteDatabase db;

	@Override
	protected void setUp() throws Exception {
		// TODO Auto-generated method stub
		super.setUp();
		helper = new MyOpenHelper(getContext(), "emp.db", null, 1);
		db = helper.getWritableDatabase();
	}

	@Override
	protected void tearDown() throws Exception {
		// TODO Auto-generated method stub
		super.tearDown();
		db.close();
	}

	public void test() {

	}

	public void insertAction() {
		db.execSQL("insert into Emp(name,salary) values('张无忌','12000')");
		db.execSQL("insert into Emp(name,salary) values('赵敏','11000')");
		db.execSQL("insert into Emp(name,salary) values('谢逊','16000')");
	}

	public void deleteAction() {
		db.execSQL("delete from Emp where name = '赵敏'");
	}

	public void updateAction() {
		db.execSQL("update Emp set salary = '18000' where name = ?",
				new Object[] { "谢逊" });
	}

	public void selectAction() {
		Cursor c = db.rawQuery("select * from Emp", null);
		while (c.moveToNext()) {
			String id = c.getString(c.getColumnIndex("id"));
			String name = c.getString(c.getColumnIndex("name"));
			String salary = c.getString(c.getColumnIndex("salary"));
			System.out.println(id + " , " + name + " , " + salary);
		}
	}

	public void insertAPI() {//添加
		ContentValues values = new ContentValues();//相当于map
		//添加的时候key一定要是Emp表中存在的字段
		values.put("name", "洪七公");
		values.put("salary", "5000");
		//insert(String table, String nullColumnHack, ContentValues values)
		db.insert("Emp", null, values);
	}

	public void deleteAPI() {//删除
		/*
		 * delete(String table, String whereClause, String[] whereArgs)
		 * whereClause 
		 * 			the optional WHERE clause to apply when deleting. Passing null will delete all rows.
		 * whereArgs 
		 * 			You may include ?s in the where clause, which will be replaced by the values from whereArgs. The values will be bound as Strings.
		 */
		int columns = db.delete("Emp", "name = ?", new String[] { "谢逊" });
		System.out.println("行数:" + columns);
	}

	public void updateAPI() {//修改
		ContentValues values = new ContentValues();
		values.put("salary", 500);
		//update(String table, ContentValues values, String whereClause, String[] whereArgs)
		int columns = db.update("Emp", values, "name = ?",new String[] { "张无忌" });
		System.out.println("行数:" + columns);
	}

	public void selectAPI() {//查询
		//query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)
		Cursor c = db.query("Emp", new String[] { "name", "salary" }," id
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇小记:获取系统时间的long值,格.. 下一篇安卓状态栏通知Status Bar Notifi..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目