在Android开发 中,数据库是不可缺少的。在Android开发中,用的是一种小型的嵌入式数据库,sqllite。今天会写两篇关于Android开发环境下数据库开发的文章,第一篇先介绍使用android自带的API来实现数据库的使用和管理以及增删改查、数据库升级的相关操作。
package com.example.freedomsql.bean; import java.io.Serializable; /** * @ClassName: Person * @author victor_freedom (x_freedom_reddevil@126.com) * @createddate 2015-1-10 下午4:22:09 * @Description: TODO */ public class Person implements Serializable { private int id; private String name; private String number; // private String nickname; public Person(int id, String name, String number) { super(); this.id = id; this.name = name; this.number = number; // this.nickname = nickname; } }
package com.example.freedomsql.db; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; /** * @ClassName: FreedomDB * @author victor_freedom (x_freedom_reddevil@126.com) * @createddate 2015-1-10 下午2:49:15 * @Description: TODO */ public class FreedomDB extends SQLiteOpenHelper { public FreedomDB(Context context) { // 参数依次为:上下文,数据库名称,游标工厂一般为NULL,数据库版本号,升级的时候需要更改 super(context, "freedom.db", null, 1); } /** * 第一次创建数据库的时候触发该方法 */ @Override public void onCreate(SQLiteDatabase db) { //sql语句详情请参考该系列文章第一篇 db.execSQL("create table person (id integer primary key autoincrement,name varchar(20),number varchar(20))"); } /** * 数据库版本号变化的时候触发该方法 */ @Override public void onUpgrade(SQLiteDatabase db, int oldversion, int newversion) { // if (oldversion == 1 && newversion == 2) { // db.execSQL("alter table person add nickname varchar(20)"); // } } }
package com.example.freedomsql.db.dao; import java.util.ArrayList; import java.util.List; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import com.example.freedomsql.bean.Person; import com.example.freedomsql.db.FreedomDB; /** * @ClassName: PersonDao * @author victor_freedom (x_freedom_reddevil@126.com) * @createddate 2015-1-10 下午2:30:24 * @Description: TODO */ public class PersonDao { private FreedomDB dbHelper; public PersonDao(Context context) { dbHelper = new FreedomDB(context); } /** * @Title: add * @Description:增加 * @param name * @param number * @throws */ public void add(String name, String number) { SQLiteDatabase db = dbHelper.getWritableDatabase(); db.execSQL("insert into person(name,number) values (?,?)", new Object[] { name, number }); db.close(); // 系统API // ContentValues = values = new ContentValues(); // values.put("name",name); // values.put("number",number); // long id = db.insert(table(表名),null,values(数据)); // return id; } // /** // * @Title: add // * @Description: 升级后的增加方法 // * @param name // * @param number // * @param nickname // * @throws // */ // public void add(String name, String number, String nickname) { // SQLiteDatabase db = dbHelper.getWritableDatabase(); // db.execSQL("insert into person(name,number,nickname) values (?,?,?)", // new Object[] { name, number, nickname }); // db.close(); // // 系统API // // ContentValues = values = new ContentValues(); // // values.put("name",name); // // values.put("number",number); // // long id = db.insert(table(表名),null,values(数据)); // // return id; // // } /** * @Title: delete * @Description: 删除 * @param name * @throws */ public void delete(String name) { SQLiteDatabase db = dbHelper.getWritableDatabase(); db.execSQL("delete from person where name=?", new Object[] { name }); // 系统API // int number=db.delete("person","name=?",new String[]{name}); db.close(); } /** * @Title: updata * @Description: 更新 * @param name * @param newnumber * @throws */ public void updata(String name, String newnumber) { SQLiteDatabase db = dbHelper.getWritableDatabase(); db.execSQL("update person set number=? where name=?", new Object[] { newnumber, name }); // 系统API // ContentValues values = new ContentValues(); // values.put("number", newnumber); // int num = db.update("person", values, "name=?", new String[] { name // }); db.close(); } /** * @Title: find * @Description: 查询 * @param name * @return * @throws */ public boolean find(String name) { SQLiteDatabase db = dbHelper.getReadableDatabase(); Cursor cursor = db.rawQuery("select * from person where name =?", new String[] { name }); // 系统API // Cursor cursor = db.query(table(表名),null,"name=?",new // String[](name),null,null,null); boolean result = cursor.moveToNext(); cursor.close(); db.close(); return result; } /** * @Title: findAll * @Description:查询 * @return * @throws */ public List<Person> findAll() { List<Person> persons = new ArrayList<Person>(); SQLiteDatabase db = dbHelper.getReadableDatabase(); Cursor cursor = db.rawQuery("select * from person", null); // 系统API // Cursor cursor= db.query("Person",new // String[]{"name","id","number"},null,null,null,null,null); while (cursor.moveToNext()) { int id = cursor.getInt(cursor.getColumnIndex("id")); String name = cursor.getString(cursor.getColumnIndex("name")); String number = cursor.getString(cursor.getColumnIndex("number")); // String nickname = cursor.getString(cursor // .getColumnIndex("nickname")); Person p = new Person(id, name, number); persons.add(p); } cursor.close(); db.close(); return persons; } }
package com.example.freedomsql; import com.example.freedomsql.db.dao.PersonDao; import android.app.Activity; import android.app.ActionBar; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.os.Build; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); PersonDao dao = new PersonDao(getBaseContext()); dao.add("汤老板", "110"); dao.add("郭大侠", "119"); // dao.add("汤老板1", "110", "汤老板"); // dao.add("郭大侠1", "119", "郭女侠"); } }
package com.example.freedomsql.bean; import java.io.Serializable; /** * @ClassName: Person * @author victor_freedom (x_freedom_reddevil@126.com) * @createddate 2015-1-10 下午4:22:09 * @Description: TODO */ public class Person implements Serializable { private int id; private String name; private String number; private String nickname; public Person(int id, String name, String number, String nickname) { super(); this.id = id; this.name = name; this.number = number; this.nickname = nickname; } }
public FreedomDB(Context context) { // 参数依次为:上下文,数据库名称,游标工厂一般为NULL,数据库版本号,升级的时候需要更改 super(context, "freedom.db", null, 2); }
/** * 数据库版本号变化的时候触发该方法 */ @Override public void onUpgrade(SQLiteDatabase db, int oldversion, int newversion) { if (oldversion == 1 && newversion == 2) { db.execSQL("alter table person add nickname varchar(20)"); } }
/** * @Title: add * @Description: 升级后的增加方法 * @param name * @param number * @param nickname * @throws */ public void add(String name, String number, String nickname) { SQLiteDatabase db = dbHelper.getWritableDatabase(); db.execSQL("insert into person(name,number,nickname) values (?,?,?)", new Object[] { name, number, nickname }); db.close(); // 系统API // ContentValues = values = new ContentValues(); // values.put("name",name); // values.put("number",number); // long id = db.insert(table(表名),null,values(数据)); // return id; }
/** * @Title: findAll * @Description:查询 * @return * @throws */ public List<Person> findAll() { List<Person> persons = new ArrayList<Person>(); SQLiteDatabase db = dbHelper.getReadableDatabase(); Cursor cursor = db.rawQuery("select * from person", null); // 系统API // Cursor cursor= db.query("Person",new // String[]{"name","id","number"},null,null,null,null,null); while (cursor.moveToNext()) { int id = cursor.getInt(cursor.getColumnIndex("id")); String name = cursor.getString(cursor.getColumnIndex("name")); String number = cursor.getString(cursor.getColumnIndex("number")); String nickname = cursor.getString(cursor .getColumnIndex("nickname")); Person p = new Person(id, name, number,nickname); persons.add(p); } cursor.close(); db.close(); return persons; }
package com.example.freedomsql; import com.example.freedomsql.db.dao.PersonDao; import android.app.Activity; import android.app.ActionBar; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.os.Build; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); PersonDao dao = new PersonDao(getBaseContext()); dao.add("汤老板1", "110", "汤老板"); dao.add("郭大侠1", "119", "郭女侠"); } }
SQL学习笔记之数据库专题(二):Android下SQL数据库开发
原文地址:http://blog.csdn.net/victorfreedom/article/details/42583625