Android SQLite操作

来源:互联网 发布:阿里云的域名如何解析 编辑:程序博客网 时间:2024/06/04 17:52

1:继承SQLiteOpenHelper:public class SQLOperation extends SQLiteOpenHelper

getReadableDatabase() 创建或者打开一个查询数据库

getWritableDatabase()创建或者打开一个可写数据库

onCreate(SQLiteDatabase):在数据库第一次创建的时候会调用这个方法,一般我们在这个方法里边创建数据库表。

onUpgrade(SQLiteDatabase,int,int):当数据库需要修改的时候,Android系统会主动的调用这个方法。一般我们在这个方法里边删除数据库表,并建立新的数据库表,当然是否还需要做其他的操作,完全取决于应用程序的需求


在SQLiteOepnHelper的子类当中,必须有以下该构造函数

public DatabaseHelper(Context context, String name, CursorFactory factory,   int version)

 {  

  //必须通过super调用父类当中的构造函数  

super(context, name, factory, version);  

}  


2:创建数据:

/**
* Create table
*/
public void onCreate(SQLiteDatabase paramSQLiteDatabase) 
{
StringBuffer localDown = new StringBuffer();
localDown.append(" Create table ");
localDown.append("location_download_resource");
localDown.append("(app_download_url varchar primary key,");
localDown.append("app_file_location varchar); ");

paramSQLiteDatabase.execSQL(localDown.toString());
}

/**
 * insert table
 */

public long insertDown(Map<String, String> info) 
{
SQLiteDatabase db = null;
long row = 0L;
try 
{
db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
Set<String> keys = info.keySet();
for (String key : keys)
{
cv.put(key, info.get(key));
}
row = db.insert(" location_download_resource ", null, cv);

catch (Exception e) 
{
CommonUtil.log("Exception:" + e.toString());
}
finally
{
if (null != db)
{
db.close();
}
}
return row;
}


public DownResource selectDownByID(String down_url) 
{
List<DownResource> list = new ArrayList<DownResource>();
DownResource resource = null;
Cursor cursor = null;
SQLiteDatabase sDatabase = null;
try 
{
sDatabase = getReadableDatabase();

cursor = sDatabase.query("location_download_resource", null, "app_download_url=?", new String[]{down_url}, null, null, " app_download_url desc "); 

if (null != cursor) 
{
while (cursor.moveToNext()) 
{
DownResource info = getDownValue(cursor);
list.add(info);
}
}

if (CommonUtil.isNotEmpty(list))
{
resource = list.get(0);
}

catch (Exception e) 
{
e.printStackTrace();

finally 
{
if (null != cursor) 
{
cursor.close();
}
if (null != sDatabase) 
{
sDatabase.close();
}
}
return resource;
}


public List<ApplicationInfo> selectInfo() 
{
List<ApplicationInfo> list = new ArrayList<ApplicationInfo>();
Cursor cursor = null;
SQLiteDatabase sDatabase = null;
try {
sDatabase = this.getReadableDatabase();
cursor = sDatabase.query(" tab_appstore_info ", null, null, null,null, null, " app_id desc ");
if (null != cursor) 
{
while (cursor.moveToNext()) 
{
ApplicationInfo info = getInfo(cursor);
if (!list.contains(info))
{
list.add(info);
}
}
}

catch (Exception e) 
{
e.printStackTrace();

finally 
{
if (null != cursor) 
{
cursor.close();
}
if (null != sDatabase) 
{
sDatabase.close();
}
}
return list;
}


/**
* update object by id

* @param id
* @param paramaters
*/
public void updateVersion(int id, Map<String, String> paramaters) 
{
SQLiteDatabase database = this.getWritableDatabase();
try 
{
String whereClause = "update_id=?";
String[] whereArgs = { Integer.toString(id) };
ContentValues cv = new ContentValues();

Set<String> keys = paramaters.keySet();
for (String key : keys)
{
cv.put(key, paramaters.get(key));
}

database.update(" tab_appstore_update ", cv, whereClause, whereArgs);

catch (Exception e)
{
e.printStackTrace();

finally 
{
if (null != database) 
{
database.close();
}
}
}

原创粉丝点击