java.lang.Object
android.database.sqlite.SQLiteQueryBuilder
public class SQLiteQueryBuilderextends Object
This is a convience class that helps build SQL queries to be sent to SQLiteDatabase objects.
构造方法摘要 | |
---|---|
SQLiteQueryBuilder() |
方法摘要 | |
---|---|
static void |
appendColumns(StringBuilder s,
String[] columns)
Add the names that are non-null in columns to s, separating them with commas. |
void |
appendWhere(CharSequence inWhere)
Append a chunk to the WHERE clause of the query. |
void |
appendWhereEscapeString(String inWhere)
Append a chunk to the WHERE clause of the query. |
String |
buildQuery(String[] projectionIn,
String selection,
String[] selectionArgs,
String groupBy,
String having,
String sortOrder,
String limit)
Construct a SELECT statement suitable for use in a group of SELECT statements that will be joined through UNION operators in buildUnionQuery. |
static String |
buildQueryString(boolean distinct,
String tables,
String[] columns,
String where,
String groupBy,
String having,
String orderBy,
String limit)
Build an SQL query string from the given clauses. |
String |
buildUnionQuery(String[] subQueries,
String sortOrder,
String limit)
Given a set of subqueries, all of which are SELECT statements, construct a query that returns the union of what those subqueries return. |
String |
buildUnionSubQuery(String typeDiscriminatorColumn,
String[] unionColumns,
Set<String> columnsPresentInTable,
int computedColumnsOffset,
String typeDiscriminatorValue,
String selection,
String[] selectionArgs,
String groupBy,
String having)
Construct a SELECT statement suitable for use in a group of SELECT statements that will be joined through UNION operators in buildUnionQuery. |
String |
getTables()
Returns the list of tables being queried |
Cursor |
query(SQLiteDatabase db,
String[] projectionIn,
String selection,
String[] selectionArgs,
String groupBy,
String having,
String sortOrder)
Perform a query by combining all current settings and the information passed into this method. |
Cursor |
query(SQLiteDatabase db,
String[] projectionIn,
String selection,
String[] selectionArgs,
String groupBy,
String having,
String sortOrder,
String limit)
Perform a query by combining all current settings and the information passed into this method. |
void |
setCursorFactory(SQLiteDatabase.CursorFactory factory)
Sets the cursor factory to be used for the query. |
void |
setDistinct(boolean distinct)
Mark the query as DISTINCT. |
void |
setProjectionMap(Map<String,String> columnMap)
Sets the projection map for the query. |
void |
setTables(String inTables)
Sets the list of tables to query. |
Android提供以下四种存储方式:
private String setCityName(String _cityName){
City.getCity().setCityName(_cityName);
Context ctx =MainActivity.this;
SharedPreferences sp =ctx.getSharedPreferences("CITY", MODE_PRIVATE);
Editor editor=sp.edit();
editor.putString("CityName", City.getCity().getCityName());
editor.commit();
return City.getCity().getCityName();
}
private String getCityName(){
String cityName = City.getCity().getCityName();
if(cityName==null ||cityName==""){
Context ctx =MainActivity.this;
SharedPreferences sp =ctx.getSharedPreferences("CITY", MODE_PRIVATE);
City.getCity().setCityName(sp.getString("CityName", "广州"));
}
return City.getCity().getCityName();
}
SQLite是一个轻量级关系型数据库,既然是关系型数据库,那操作起来其实跟mysql、sql server差不多的。需要注意的一点是,SQLite只有NULL、INTEGER、REAL(浮点数)、TEXT(字符串)和BLOB(大数据)五种类型,不存在BOOLEAN和DATE类型。
String sql="create table t_user (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL,password TEXT NOT NULL)";
db.execSQL(sql);
ContentValues values=new ContentValues();
values.put("name", "liangjh");
values.put("password", "123456");
db.insert("t_user", "id", values);
// 方式1 直接将条件写入到条件里面(个人觉得容易被注入,但其实数据都在客户端,没啥安全性可言)
db.delete("t_user", "id=1", null);
// 方式2 条件分开写,感觉比较安全
db.delete("t_user", "name=? and password =?", new String[]{"weiyg","112233"});
// 使用rawQuery
// Cursor c = db.rawQuery("select * from t_user", null);
// db.rawQuery("select * from t_user where id=1", null);
// db.rawQuery("select * from t_user where id=?", new String[]{"1"});
// 使用query()
Cursor c = db.query("t_user", new String[]{"id","name"}, "name=?", new String[]{"weiyg"}, null, null, null);
c.moveToFirst();
while(!c.isAfterLast()){
String msg="";
for(int i=0,j=c.getColumnCount();i<j;i++){
msg+="--"+c.getString(i);
}
Log.v("SQLite", "data:"+msg);
c.moveToNext();
}
ContentValues values=new ContentValues();
values.put("password", "111111");
// 方式1 条件写在字符串内
db.update("t_user", values, "id=1", null);
// 方式2 条件和字符串分开
db.update("t_user", values, "name=? or password=?",new String[]{"weiyg","123456"});
无论何时,打开的数据库,记得关闭。
db.close()
另外使用beginTransaction()和endTransaction()可以设置事务。
1 public class MyProvider extends ContentProvider {
2
3 @Override
4 public int delete(Uri uri, String selection, String[] selectionArgs) {
5 // TODO Auto-generated method stub
6 return 0;
7 }
8
9 @Override
10 public String getType(Uri uri) {
11 // TODO Auto-generated method stub
12 return null;
13 }
14
15 @Override
16 public Uri insert(Uri uri, ContentValues values) {
17 // TODO Auto-generated method stub
18 return null;
19 }
20
21 @Override
22 public boolean onCreate() {
23 // 新建个数据库并插入一条数据
24 SQLiteDatabase db=this.getContext().openOrCreateDatabase("test_db2.db", Context.MODE_PRIVATE, null);
25 db.execSQL("CREATE TABLE t_user (id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT NOT NULL)");
26 ContentValues values=new ContentValues();
27 values.put("name", "liangjh2");
28 db.insert("t_user", "id", values);
29 db.close();
30 return false;
31 }
32
33 @Override
34 public Cursor query(Uri uri, String[] projection, String selection,
35 String[] selectionArgs, String sortOrder) {
36 // 获取数据
37 SQLiteDatabase db=this.getContext().openOrCreateDatabase("test_db2.db", Context.MODE_PRIVATE, null);
38 Cursor c = db.query("t_user", null, null, null, null, null, null);
39 db.close();
40 return c;
41 }
42
43 @Override
44 public int update(Uri uri, ContentValues values, String selection,
45 String[] selectionArgs) {
46 // TODO Auto-generated method stub
47 return 0;
48 }
49
50 }
<application ...>
...
<provider android:name=".MyProvider" android:authorities="com.example.androidtestdemo" android:exported="true"/>
</application>
Context ctx=MainActivity.this;
ContentResolver resolver =ctx.getContentResolver();
Uri uri=Uri.parse("content://com.example.androidtestdemo");
Cursor c = resolver.query(uri, null, null, null, null);
c.moveToFirst();
while(!c.isAfterLast()){
for(int i=0,j=c.getColumnCount();i<j;i++){
Log.v("Android2",""+c.getString(i));
}
c.moveToNext();
}
原文:http://www.cnblogs.com/ZhuRenWang/p/4859848.html