1.实现SQLiteHelper来在android中使用SQLite.代码如下,来自android官网。

public class FeedReaderDbHelper extends SQLiteOpenHelper {
// If you change the database schema, you must increment the database version.
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "FeedReader.db"; public FeedReaderDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
} /*(必须重写的方法)
该函数是在第一次创建数据库的时候执行,实际上是在第一次得到SQLiteDatabase对象的时候,才会调用这个方法
* 在SQLiteOpenHelper的getReaderDatabase()或者getWritableDatabase()
* 被第一次调用时都会调用该方法
*/
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_ENTRIES);
}
//(必须重写的方法)数据库更新的时候会调用该方法
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// This database is only a cache for online data, so its upgrade policy is
// to simply to discard the data and start over
db.execSQL(SQL_DELETE_ENTRIES);
onCreate(db);
}
//可以不用重写
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onUpgrade(db, oldVersion, newVersion);
}
}

其中onUpgrade是:

当我们已经有数据库存在,想保存原有数据,只想增加一个字段等,只需修改版本号,

SQLiteOpenHelper会自动判断版本号是否一致,假如不一致会自动调用onUpgrade函数

通过重载这个函数,你可以做任何你想做的事,包括增、删字段等等~

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("ALTER TABLE user_table ADD age INTEGER DEFAULT 0");
}

2.插入数据

DatabaseHelper dbHelper = new DatabaseHelper(getApplicationContext());
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues cv = new ContentValues();
//key是列名,value是要插入的值
cv.put("name", "value");
//rowId是返回的新插入的行号,如果插入失败返回-1
long rowId = db.insert("user_table", null, cv);

其中db.insert("user_table", "column_name", cv);第一个参数是要插入的表名。第二个参数是一个列名,当ContentValues对象为null或者size为0时,该列的值为null。

因为每次调用insert都必须执行insert into tableName(column_name)values(value),当ContentValues的对象为null等,则会执行insert into tableName(column_name)values(null).

若不为空,则执行正常的插入语句

3.查数据

SQLiteDatabase db = mDbHelper.getReadableDatabase();

// Define a projection that specifies which columns from the database
// you will actually use after this query.
//需要查询返回的列名
String[] projection = {
FeedEntry._ID,
FeedEntry.COLUMN_NAME_TITLE,
FeedEntry.COLUMN_NAME_UPDATED,
...
}; // How you want the results sorted in the resulting Cursor
//查询语句排序
String sortOrder =
FeedEntry.COLUMN_NAME_UPDATED + " DESC"; Cursor c = db.query(
FeedEntry.TABLE_NAME, // The table to query
projection, // The columns to return
selection, // The columns for the WHERE clause
selectionArgs, // The values for the WHERE clause
null, // don't group the rows
null, // don't filter by row groups
sortOrder // The sort order
);
cursor.moveToFirst();
long itemId = cursor.getLong(
cursor.getColumnIndexOrThrow(FeedEntry._ID)
);

4.SQLiteDatabase.getWritableDatabase或者getReadableDatabase,如果没有创建数据库,会调用onCreate()

最新文章

  1. 【BZOJ-4380】Myjnie 区间DP
  2. ubuntu12.04中shell脚本无法使用source的原因及解决方法
  3. 在Heroku部署时,无法加载 css,js,图片资源解决办法
  4. angular中ng-repeat ng-if 中的变量的值控制器中为什么取不到
  5. Debian 7环境安装TightVNC+Gnome远程桌面环境
  6. POJ3013 Big Christmas Tree[转换 最短路]
  7. 在Eclipse中编写servlet时出现"The import javax.servlet cannot be resolved" 问题解决办法
  8. win7/8下VirtualBox虚拟共享文件夹设置
  9. jquery图片无缝滚动代码左右 上下无缝滚动图片
  10. 3、bootstrap3.0 栅格偏移 布局中的一个特产
  11. javascript date部分
  12. 第三次冲刺spring会议(第一次会议)
  13. GoF 设计模式:浅浅印象
  14. VMware下ubuntu与Windows实现文件共享的方法
  15. cocos2dx3.0导出自定义类到lua的方法详细步骤
  16. .net后台防止API接口被重复请求
  17. python-类的定制
  18. 【转载】FPGA 中的latch 锁存器
  19. 史上最全!Selenium元素定位的30种方式
  20. (整理)MySQL_REHL6.5 安装MySQL5.5

热门文章

  1. POJ1151+线段树+扫描线
  2. ABC: Always Be Coding——程序员面试必
  3. highcharts 柱形堆叠图
  4. Android view的requestLayout()
  5. 比nerdtree更好的文件浏览器:vimfiler
  6. ObfuscationAttribute模糊处理
  7. 安装win7 32位系统出现的问题解决办法
  8. html树形菜单控件
  9. Android开发之bug-No Activity found to handle Intent
  10. JS 动态显示 获取下拉框的多个值