sqliteOpenHelper常用方法:
DBHelper.Java
<span >import androID.content.Context;import androID.database.sqlite.sqliteDatabase;import androID.database.sqlite.sqliteOpenHelper;/** * Created by tangjiarao on 16/9/7. */public class DbHelper extends sqliteOpenHelper{ private static final String DATABASE_name = "person.db"; //数据库名称 private static final int DATABASE_VERSION = 1;//数据库版本 public DbHelper(Context context){ super(context,DATABASE_name,null,DATABASE_VERSION); } public voID onCreate(sqliteDatabase db) { db.execsql("CREATE table person " + "(_ID integer primary key autoincrement," + "name varchar(20)," + "age varchar(10))"); } @OverrIDe public voID onUpgrade(sqliteDatabase db,int i,int i1) { db.execsql("DROP table IF EXISTS person"); onCreate(db); }}</span>PersonService.Java
<span >import androID.content.Context;import androID.database.Cursor;import androID.database.sqlite.sqliteDatabase;import java.util.ArrayList;import java.util.List;import tplusr.storagetest.db.DbHelper;public class PersonService { private DbHelper dbHelper; private sqliteDatabase db; public PersonService(Context context) { dbHelper=new DbHelper(context); db=dbHelper.getReadableDatabase(); } /** * 插入方法 * @param person */ public voID insert(Person person){ sqliteDatabase db=dbHelper.getWritableDatabase(); db.execsql("insert into person(name,age) values(?,?)",new Object[]{person.getname(),person.getAge()}); } /** * 删除方法 * @param _ID */ public voID delete(Integer _ID){ sqliteDatabase db=dbHelper.getWritableDatabase(); db.execsql("delete from person where _ID=?",new Object[]{_ID}); } /** * 查找某行数据方法 * @param _ID * @return */ public Person find(Integer _ID){ sqliteDatabase db=dbHelper.getWritableDatabase(); Cursor cursor=db.rawquery("select * from person where _ID=?",new String[]{_ID.toString()}); if(cursor.movetoFirst()){ int ID = cursor.getInt(cursor.getColumnIndex("_ID")); String name = cursor.getString(cursor.getColumnIndex("name")); String age = cursor.getString(cursor.getColumnIndex("age")); Person person = new Person(); person.set_ID(ID); person.setname(name); person.setAge(age); return person; } return null; } /** * 查找所有数据方法 * @return */ public List<Person> findAll(){ sqliteDatabase db=dbHelper.getReadableDatabase(); List<Person> persons = new ArrayList<Person>(); Cursor cursor=db.rawquery("select * from person",null); while(cursor.movetoNext()){ Person person=new Person(); int ID=cursor.getInt(cursor.getColumnIndex("_ID")); String name=cursor.getString(cursor.getColumnIndex("name")); String age=cursor.getString(cursor.getColumnIndex("age")); person.set_ID(ID); person.setname(name); person.setAge(age); persons.add(person); } return persons; }}</span>Person.Java
<span >public class Person { private int _ID; private String name; private String age; public int get_ID() { return _ID; } public voID set_ID(int _ID) { this._ID = _ID; } public String getname() { return name; } public voID setname(String name) { this.name = name; } public String getAge() { return age; } public voID setAge(String age) { this.age = age; }}</span>
DBActivity.Java
总结<span >import androID.support.v7.app.AppCompatActivity;import androID.os.Bundle;import androID.util.Log;import java.util.List;import tplusr.storagetest.R;public class DBActivity extends AppCompatActivity { @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_db); PersonService service =new PersonService(this); Person person =new Person(); person.setname("ahdad"); person.setAge("39"); service.insert(person); List<Person> p =service.findAll(); for (int i =0;i<p.size();i++){ Log.d("db",p.get(i).toString()); } }}</span>结果:
以上是内存溢出为你收集整理的温故知新----5种数据存储方式之SQLiteOpenHelper全部内容,希望文章能够帮你解决温故知新----5种数据存储方式之SQLiteOpenHelper所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)