知识点详解:http://www.jb51.cc/article/p-kfcfzbzb-bv.html
具体代码:
MainActivity.javapackage com.example.androID_sqlite;import androID.app.Activity;import androID.os.Bundle;import com.example.androID_sqlite.dao.impl.UserDaoImpls;import com.example.androID_sqlite.database.DatabaseHelper;public class MainActivity extends Activity { private UserDaoImpls userDaoImpls; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); // 创建数据库 DatabaseHelper dh = new DatabaseHelper(this); userDaoImpls = new UserDaoImpls(dh); }}
Users.java
package com.example.androID_sqlite.domain;import java.io.Serializable;public class Users implements Serializable{ private static final long serialVersionUID = 1L; private Integer userID; private String username; private Integer userAge; private Double userSalary; public Users() { super(); } public Users(Integer userID,String username,Integer userAge,Double userSalary) { super(); this.userID = userID; this.username = username; this.userAge = userAge; this.userSalary = userSalary; } public Integer getUserID() { return userID; } public voID setUserID(Integer userID) { this.userID = userID; } public String getUsername() { return username; } public voID setUsername(String username) { this.username = username; } public Integer getUserAge() { return userAge; } public voID setUserAge(Integer userAge) { this.userAge = userAge; } public Double getUserSalary() { return userSalary; } public voID setUserSalary(Double userSalary) { this.userSalary = userSalary; } @OverrIDe public String toString() { return "Users [userID=" + userID + ",username=" + username + ",userAge=" + userAge + ",userSalary=" + userSalary + "]"; } }
BaseDao.java
package com.example.androID_sqlite.dao;import java.util.List;public interface BaseDao<T,PK> { /** * 插入实体对象 * * @param entity * @return */ public boolean insert(T entity); /** * 更新实体对象 * * @param entity * @return */ public boolean update(T entity); /** * 删除实体对象 * * @param entity * @return */ public boolean delete(T entity); /** * 根据ID删除对象 * * @param ID * @return */ public boolean deleteByID(PK ID); /** * 根据ID查找对象 * * @param ID * @return */ public T findByID(PK ID); /** * 查找所有 * * @return */ public List<T> findAll();}
UserDao.java
package com.example.androID_sqlite.dao;import com.example.androID_sqlite.domain.Users;public interface UserDao extends BaseDao<Users,Integer>{}
UserDaoImple.java
package com.example.androID_sqlite.dao.impl;import java.util.ArrayList;import java.util.List;import androID.database.Cursor;import androID.database.sqlite.sqliteDatabase;import com.example.androID_sqlite.dao.UserDao;import com.example.androID_sqlite.database.DatabaseHelper;import com.example.androID_sqlite.domain.Users;public class UserDaoImpls implements UserDao { // 依赖对象 private DatabaseHelper dh; // 通过数据库的构造器实例化 public UserDaoImpls(DatabaseHelper databaseHelper) { this.dh = databaseHelper; } @OverrIDe public boolean insert(Users entity) { // 得到数据库 *** 作对象 try { sqliteDatabase db = dh.getWritableDatabase(); /* * 方法一 * db.execsql("insert into users(username,userage,usersalary) values('" * + entity.getUsername() + "'," + entity.getUserAge() + "," + * entity.getUserSalary() + ")"); */ // 方法二 db.execsql( "insert into users(username,usersalary) values(?,?,?)",new Object[] { entity.getUsername(),entity.getUserAge(),entity.getUserSalary() }); db.close(); } catch (Exception e) { e.printstacktrace(); return false; } return true; } @OverrIDe public boolean update(Users entity) { try { sqliteDatabase db = dh.getWritableDatabase(); db.execsql( "update users set username=?,userage=?,usersalary=? where userID=?",entity.getUserSalary(),entity.getUserID() }); db.close(); } catch (Exception e) { e.printstacktrace(); return false; } return true; } @OverrIDe public boolean delete(Users entity) { return deleteByID(entity.getUserID()); } @OverrIDe public boolean deleteByID(Integer ID) { try { sqliteDatabase db = dh.getWritableDatabase(); db.execsql("delete from users where userID=?",new Object[] { ID }); db.close(); } catch (Exception e) { e.printstacktrace(); return false; } return true; } @OverrIDe public Users findByID(Integer ID) { Users entity = null; sqliteDatabase db = dh.getWritableDatabase(); Cursor c = db .rawquery( "select userID,username,usersalary from users where userID=?",new String[] { ID + "" }); if (c.movetoNext()) { entity = new Users(); entity.setUserID(c.getInt(c.getColumnIndex("userID"))); entity.setUsername(c.getString(c.getColumnIndex("userID"))); entity.setUserAge(c.getInt(c.getColumnIndex("userage"))); entity.setUserSalary(c.getDouble(c.getColumnIndex("usersalary"))); } return entity; } @OverrIDe public List<Users> findAll() { List<Users> entitIEs = new ArrayList<Users>(); sqliteDatabase db = dh.getWritableDatabase(); Cursor c = db .rawquery( "select userID,usersalary from users",null); while (c.movetoNext()) { Users entity = new Users(); entity.setUserID(c.getInt(c.getColumnIndex("userID"))); entity.setUsername(c.getString(c.getColumnIndex("userID"))); entity.setUserAge(c.getInt(c.getColumnIndex("userage"))); entity.setUserSalary(c.getDouble(c.getColumnIndex("usersalary"))); entitIEs.add(entity); } return entitIEs; }}
DatabaseTese.java
package com.example.androID_sqlite.test;import java.util.List;import com.example.androID_sqlite.dao.UserDao;import com.example.androID_sqlite.dao.impl.UserDaoImpls;import com.example.androID_sqlite.database.DatabaseHelper;import com.example.androID_sqlite.domain.Users;import androID.test.AndroIDTestCase;public class DatabaseTeses extends AndroIDTestCase { public voID createDatabase() { DatabaseHelper dh = new DatabaseHelper(getContext()); dh.getWritableDatabase(); } public voID insert() { DatabaseHelper databaseHelper = new DatabaseHelper(getContext()); UserDao userDao = new UserDaoImpls(databaseHelper); Users entity = new Users(null,"iii2",20,4000.0); boolean flag = userDao.insert(entity); assertEquals(true,flag); } public voID update() { DatabaseHelper databaseHelper = new DatabaseHelper(getContext()); UserDao userDao = new UserDaoImpls(databaseHelper); Users entity = new Users(7,"aaa",21,3000.0); boolean flag = userDao.update(entity); assertEquals(true,flag); } public voID delete() { DatabaseHelper databaseHelper = new DatabaseHelper(getContext()); UserDao userDao = new UserDaoImpls(databaseHelper); Users entity = new Users(1,"",null,null); boolean flag = userDao.delete(entity); assertEquals(true,flag); } public voID findByID() { DatabaseHelper databaseHelper = new DatabaseHelper(getContext()); UserDao userDao = new UserDaoImpls(databaseHelper); Users entity = userDao.findByID(3); if (entity != null) { System.out.println(entity.toString()); } else { System.out.println("no message"); } } public voID findAll() { DatabaseHelper databaseHelper = new DatabaseHelper(getContext()); UserDao userDao = new UserDaoImpls(databaseHelper); List<Users> entitIEs = userDao.findAll(); for (Users entity : entitIEs) { System.out.println(entity.toString()); } }}总结
以上是内存溢出为你收集整理的赵雅智_使用SQLiteDatabase *** 作SQLite数据库及事务全部内容,希望文章能够帮你解决赵雅智_使用SQLiteDatabase *** 作SQLite数据库及事务所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)