惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
I
InfoQ
Engineering at Meta
Engineering at Meta
D
DataBreaches.Net
L
LangChain Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Recent Announcements
Recent Announcements
GbyAI
GbyAI
爱范儿
爱范儿
Microsoft Security Blog
Microsoft Security Blog
腾讯CDC
美团技术团队
罗磊的独立博客
Microsoft Azure Blog
Microsoft Azure Blog
WordPress大学
WordPress大学
T
The Blog of Author Tim Ferriss
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
雷峰网
雷峰网
M
MIT News - Artificial intelligence
D
Docker
MongoDB | Blog
MongoDB | Blog
F
Fortinet All Blogs
博客园 - 叶小钗

博客园 - Liren

查找目录中同名的文件或者文件夹 获取设置一个字节某一个位的数值 基于JMS的数据交换既数据互操作平台的解决方案 Spring MVC基于注解的Junit测试 Spring MVC 3中关于url-pattern设成"/"后,资源访问问题 博文阅读密码验证 - 博客园 NotificationManager - Liren - 博客园 发送短信 调用系统联系人列表 MorseCodeConverter 自用留存 - Liren - 博客园 Android 给自己的类加个事件 用代码旋转屏幕 - Liren - 博客园 Cassandra API60 Java 代码示例 Cassandra Java 使用TimeUUIDType 转:在 CLI 中練習 Data Model asp.net 实现一个简单CAS Server - Liren Ajax跨域访问代理类,支持GET和POST方法 Python 学习笔记:需要仔细阅读一个函数 Python 学习笔记: 备份工具
Android SQLiteHelper
Liren · 2010-12-11 · via 博客园 - Liren

 1 package com.liren.news.data;
 2 
 3 import android.content.ContentValues;
 4 import android.content.Context;
 5 import android.database.Cursor;
 6 import android.database.sqlite.SQLiteDatabase;
 7 
 8 public class SQLiteHelper extends android.database.sqlite.SQLiteOpenHelper {
 9 
10     private final static String DATABASE_NAME = "db_name";
11     private final static int DATABASE_VERSION = 1;
12     private final static String TABLE_NAME = "mytable";
13     private final static String FIELD_ID = "ID";
14     private final static String FIELD_NAME = "NAME";
15 
16     public SQLiteHelper(Context context) {
17         super(context, DATABASE_NAME, null, DATABASE_VERSION);
18     }
19 
20     @Override
21     public void onCreate(SQLiteDatabase db) {
22         String sql = "Create table %s (%s integer primary key autoincrement,%s text);";
23         sql = String.format(sql, TABLE_NAME, FIELD_ID, FIELD_NAME);
24         db.execSQL(sql);
25     }
26 
27     @Override
28     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
29         String sql = "DROP TABLE IF EXISTS " + TABLE_NAME;
30         db.execSQL(sql);
31         onCreate(db);
32     }
33 
34     public Cursor select() {
35         SQLiteDatabase db = this.getReadableDatabase();
36         Cursor cursor = db.query(TABLE_NAME, nullnullnullnullnull,
37                 FIELD_ID);
38         return cursor;
39 
40     }
41 
42     public long insert(String name) {
43         SQLiteDatabase db = this.getWritableDatabase();
44         ContentValues cv = new ContentValues();
45         cv.put(FIELD_NAME, name);
46         long row = db.insert(TABLE_NAME, null, cv);
47         return row;
48     }
49 
50     public void delete(int id){
51         SQLiteDatabase db = this.getWritableDatabase();
52         String where  = FIELD_ID + "= ?";
53         String[] wherevalue = {Integer.toString(id)};
54         db.delete(TABLE_NAME, where, wherevalue);
55     }
56     
57     public void update(int id,String name)
58     {
59         SQLiteDatabase db = this.getWritableDatabase();
60         String where  = FIELD_ID + "= ?";
61         String[] wherevalue = {Integer.toString(id)};
62         ContentValues cv = new ContentValues();
63         cv.put(FIELD_NAME, name);
64         db.update(TABLE_NAME, cv, where, wherevalue);
65     }    
66 }
67