在android开发中还有哪些技术可以方便的操作数据库,我不大清楚,今天学习了一下 ormlite,觉得还不错,非常方便。
ormlite官网下载:http://ormlite.com/releases/
package com.example.aandroidtest; import com.j256.ormlite.field.DatabaseField; import com.j256.ormlite.table.DatabaseTable; @DatabaseTable public class User { @DatabaseField(generatedId=true)//自增长的主键 private int id; @DatabaseField private String userName; @DatabaseField private String password; //必须有一个无参的构造函数 public User() { super(); } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
package com.example.aandroidtest; import java.sql.SQLException; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper; import com.j256.ormlite.dao.Dao; import com.j256.ormlite.support.ConnectionSource; import com.j256.ormlite.table.TableUtils; public class DBHelper extends OrmLiteSqliteOpenHelper { /*一张表对应一个dao*/ private Dao<User, Integer> userDao; private final static int DB_VERSION = 1; private static final String DB_NAME = "orm"; /**构造函数,貌似是必须有的 * @param context */ public DBHelper(Context context) { super(context, DB_NAME, null, DB_VERSION); } @Override public void onCreate(SQLiteDatabase arg0, ConnectionSource arg1) { try { TableUtils.createTable(arg1, User.class); } catch (SQLException e) { e.printStackTrace(); } } @Override public void onUpgrade(SQLiteDatabase arg0, ConnectionSource arg1, int arg2, int arg3) { try { TableUtils.dropTable(arg1, User.class, true); this.onCreate(arg0, arg1); } catch (SQLException e) { e.printStackTrace(); } } public Dao<User, Integer> getUserDao() throws SQLException { if(userDao == null) { userDao = getDao(User.class); } return userDao; } }
package com.example.aandroidtest; import java.sql.SQLException; import java.util.List; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.os.Bundle; import android.view.Menu; import com.j256.ormlite.android.apptools.OrmLiteBaseActivity; import com.j256.ormlite.dao.Dao; public class MainActivity extends OrmLiteBaseActivity<DBHelper> { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); try { //先获取userDao Dao<User, Integer> userDao = this.getHelper().getUserDao(); //增 User user = new User(); user.setUserName("qiantu"); user.setPassword("000000"); userDao.create(user); //删 userDao.deleteById(1); //查 String str = ""; List<User> list = userDao.queryForAll(); // List<User> list = userDao.queryForEq("id", 2); for(User u : list) { str += "id="+String.valueOf(u.getId())+"\tusername=" +u.getUserName()+"\tpassword="+u.getPassword()+"\n"; } //显示dialog查看数据 new AlertDialog.Builder(this) .setTitle("提示") .setMessage(str) .setNegativeButton("取消", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }) .show(); //改 User u = userDao.queryForId(3); u.setUserName("我已经该了名字"); userDao.update(u); } catch (SQLException e) { e.printStackTrace(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }