下面是免费的sqlite的工具及注册码,可以实现在windows下操作sqlite数据库
http://download.csdn.net/detail/itjavawfc/8098051
一些知识点:其实和平常用到的数据库没有多大区别。
1. 加载驱动.
2. 连接数据库.
3. 操作数据库.
创建表:
create table person(
_id integer primary key,
name varchar(20),
age integer
);
添加: insert into person(name,age) values('lisi',19);
删除: delete from person where _id = 1;
修改: update person set name = '李四' where name = '王五';
查询所有: select * from person;
查询单条: select * from person where name = 'zhangsan';
创建数据库的过程,在一个类的构造方法中,该类继承了sqliteOpenHelper
/* * 数据库的构造函数 * name:数据库名称 * factory 游标工程 * version 数据库版本号,不可以小于1 */ public PersonsqliteOpenHelper(Context context) { super(context,"itheima28.db",null,1); // TODO Auto-generated constructor stub }
下面是一个创建数据库的工具类:在构造方法中实现创建数据库,在程序调用的时候,在onCreate方法中创建表
/* * 数据库帮助类,用于创建和管理数据库的 */ public class PersonsqliteOpenHelper extends sqliteOpenHelper{ /* * 数据库的构造函数 * name:数据库名称 * factory 游标工程 * version 数据库版本号,不可以小于1 */ public PersonsqliteOpenHelper(Context context) { super(context,1); // TODO Auto-generated constructor stub } /* * 数据库第一次创建时回调此方法 * 初始化一些表 * (non-Javadoc) * @see android.database.sqlite.sqliteOpenHelper#onCreate(android.database.sqlite.sqliteDatabase) */ @Override public void onCreate(sqliteDatabase db) { // TODO Auto-generated method stub //操作数据库 String sql="create table person(_id integer primary key,name char(20),age integer);"; db.execsql(sql); } /* * 数据库版本号更新时回调此方法 * 删除表,更新表 * (non-Javadoc) * @see android.database.sqlite.sqliteOpenHelper#onUpgrade(android.database.sqlite.sqliteDatabase,int,int) */ @Override public void onUpgrade(sqliteDatabase db,int oldVersion,int newVersion) { // TODO Auto-generated method stub } }
实现增删改查数据库表的DAO,非常简单:
package com.itheima28.sqlitedemo.dao; import java.util.ArrayList; import java.util.List; import com.itheima28.sqlitedemo.db.PersonsqliteOpenHelper; import com.itheima28.sqlitedemo.entities.Person; import android.content.Context; import android.database.Cursor; import android.database.sqlite.sqliteDatabase; public class PersonDao { private PersonsqliteOpenHelper mOpenHelper; // 数据库的帮助对象 public PersonDao(Context context) { mOpenHelper = new PersonsqliteOpenHelper(context); } /* * 添加到person表一条数据 */ public void insert(Person person) { sqliteDatabase db = mOpenHelper.getWritableDatabase(); if (db.isOpen()) { // 判断数据库是否打开 // 执行添加数据库操作 // db.execsql("insert into person(name,19);"); db.execsql("insert into person(name,age) values(?,?);",new Object[] { person.getName(),person.getAge() }); db.close(); // 数据库关闭 } } public void delete(int id) { // 获得可写的数据库对象 sqliteDatabase db = mOpenHelper.getWritableDatabase(); if (db.isOpen()) { db.execsql("delete from person where _id=?;",new Integer[] { id }); db.close(); // 数据库关闭 } } public void update(int id,String name) { sqliteDatabase db = mOpenHelper.getWritableDatabase(); if (db.isOpen()) { if (db.isOpen()) { db.execsql("update person set name=? where _id=?;",new Object[] { name,id }); db.close(); // 数据库关闭 } } } public List<Person> queryAll() { sqliteDatabase db = mOpenHelper.getWritableDatabase(); if (db.isOpen()) { Cursor cursor = db .rawQuery("select _id,name,age from person",null); List<Person> personList = new ArrayList(); if (cursor != null && cursor.getCount() >= 0) { int id; int age; String name; while (cursor.moveToNext()) { id = cursor.getInt(0); // 取第零列的数据 name = cursor.getString(1); age = cursor.getInt(2); personList.add(new Person(id,age)); } db.close(); return personList; } db.close(); } return null; } public Person queryItem(int id) { sqliteDatabase db = mOpenHelper.getWritableDatabase(); if (db.isOpen()) { Cursor cursor = db.rawQuery( "select _id,age from person where _id=?",new String[] { id + "" }); if (cursor != null && cursor.moveToFirst()) { int _id = cursor.getInt(0); String name = cursor.getString(1); int age = cursor.getInt(2); db.close(); return new Person(_id,age); } db.close(); } return null; } }
实体类:
package com.itheima28.sqlitedemo.entities; public class Person { private int id; private String name; private int age; public Person(int id,String name,int age) { super(); this.id = id; this.name = name; this.age = age; } @Override public String toString() { return "Person [id=" + id + ",name=" + name + ",age=" + age + "]"; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
测试:
package com.itheima28.sqlitedemo.test; import java.util.List; import com.itheima28.sqlitedemo.dao.PersonDao; import com.itheima28.sqlitedemo.db.PersonsqliteOpenHelper; import com.itheima28.sqlitedemo.entities.Person; import android.test.AndroidTestCase; import android.util.Log; public class TestCase extends AndroidTestCase { private static final String TAG = "TestCase"; public void test(){ //数据库什么时候创建 PersonsqliteOpenHelper openHelper=new PersonsqliteOpenHelper(getContext()); //第一次连接数据库是创建数据库文件,会执行onCreate方法 openHelper.getReadableDatabase(); } public void testInsert(){ PersonDao dao=new PersonDao(getContext()); dao.insert(new Person(5,"冠希",8)); dao.insert(new Person(6,"姚明",11)); dao.insert(new Person(8,"国家",78)); } public void testDelete(){ PersonDao dao=new PersonDao(getContext()); dao.delete(1); } public void testUpdate(){ PersonDao dao=new PersonDao(getContext()); dao.update(1,"凤姐"); } public void testQueryAll(){ PersonDao dao=new PersonDao(getContext()); List<Person> personList=dao.queryAll(); for(Person person:personList){ Log.i(TAG,person.toString()); } } public void testQueryItem(){ PersonDao dao=new PersonDao(getContext()); Person person=dao.queryItem(1); Log.i(TAG,person.toString()); } }