Android入门数据库支持SQLite 内容提供器ContentProviderWord文档下载推荐.docx
《Android入门数据库支持SQLite 内容提供器ContentProviderWord文档下载推荐.docx》由会员分享,可在线阅读,更多相关《Android入门数据库支持SQLite 内容提供器ContentProviderWord文档下载推荐.docx(20页珍藏版)》请在冰豆网上搜索。
orientation="
vertical"
android:
TextView
id="
@+id/txtMsg"
wrap_content"
/>
Button
@+id/btn1"
@+id/btn2"
@+id/btn3"
@+id/btn4"
@+id/btn5"
@+id/btn6"
/LinearLayout>
/ScrollView>
DatabaseHelper.java
代码
importandroid.content.Context;
importandroid.database.sqlite.SQLiteDatabase;
importandroid.database.sqlite.SQLiteOpenHelper;
importandroid.database.sqlite.SQLiteDatabase.CursorFactory;
publicclassDatabaseHelperextendsSQLiteOpenHelper{
publicDatabaseHelper(Contextcontext,Stringname,CursorFactoryfactory,
intversion){
super(context,name,factory,version);
}
@Override
//创建数据库后,对数据库的操作
publicvoidonCreate(SQLiteDatabasedb){
//更改数据库版本的操作
publicvoidonUpgrade(SQLiteDatabasedb,intarg1,intarg2){
//每次成功打开数据库后首先被执行
publicvoidonOpen(SQLiteDatabasedb){
super.onOpen(db);
}
Main.java
importjava.util.Random;
importandroid.app.Activity;
importandroid.content.ContentValues;
importandroid.database.Cursor;
importandroid.database.SQLException;
importandroid.os.Bundle;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.widget.Button;
importandroid.widget.TextView;
publicclassMainextendsActivity{
privateDatabaseHelperdbHelper;
privatestaticfinalStringDATABASE_NAME="
db.db"
;
privatestaticfinalintDATABASE_VERSION=1;
privatestaticfinalStringTABLE_NAME="
employee"
TextViewtxtMsg;
/**Calledwhentheactivityisfirstcreated.*/
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dbHelper=newDatabaseHelper(this,DATABASE_NAME,null,
DATABASE_VERSION);
txtMsg=(TextView)findViewById(R.id.txtMsg);
Buttonbutton1=(Button)findViewById(R.id.btn1);
button1.setText("
创建表"
);
button1.setOnClickListener(newOnClickListener(){
@Override
publicvoidonClick(Viewv){
CreateTable();
}
});
Buttonbutton2=(Button)findViewById(R.id.btn2);
button2.setText("
插入3条记录"
button2.setOnClickListener(newOnClickListener(){
insertItem();
Buttonbutton3=(Button)findViewById(R.id.btn3);
button3.setText("
删除全部数据!
"
button3.setOnClickListener(newOnClickListener(){
deleteItem();
Buttonbutton4=(Button)findViewById(R.id.btn4);
button4.setText("
更新指定数据"
button4.setOnClickListener(newOnClickListener(){
updateItem();
Buttonbutton5=(Button)findViewById(R.id.btn5);
button5.setText("
显示全部数据"
button5.setOnClickListener(newOnClickListener(){
showItems();
Buttonbutton6=(Button)findViewById(R.id.btn6);
button6.setText("
删除表"
button6.setOnClickListener(newOnClickListener(){
dropTable();
/**
*创建表格
*/
privatevoidCreateTable(){
SQLiteDatabasedb=dbHelper.getWritableDatabase();
Stringsql="
CREATETABLEIFNOTEXISTS"
+TABLE_NAME
+"
(IDINTEGERPRIMARYKEY,NameVARCHAR,AgeINTEGER);
try{
db.execSQL(sql);
txtMsg.append("
数据表成功创建\n"
}catch(SQLExceptionex){
数据表创建错误\n"
+ex.toString()+"
\n"
}
*插入数据
privatevoidinsertItem(){
Randomrandom=newRandom();
for(inti=0;
i<
3;
i++){
Stringsql="
insertinto"
+"
(name,age)values('
name"
+String.valueOf(i)
'
"
+random.nextInt()+"
)"
db.execSQL(sql);
成功插入3条数据\n"
}catch(Exceptione){
+e.toString()+"
*删除数据
privatevoiddeleteItem(){
SQLiteDatabasedb=dbHelper.getWritableDatabase();
db.delete(TABLE_NAME,"
id<
999"
null);
数据删除成功!
数据删除失败!
*更新数据
privatevoidupdateItem(){
ContentValuesvalues=newContentValues();
values.put("
"
批量更新后的名字"
db.update(TABLE_NAME,values,"
id<
newString[]{"
100"
});
数据更新成功\n"
数据更新失败\n"
*查询数据
privatevoidshowItems(){
Stringcolumn[]={"
id"
age"
};
Cursorcursor=db.query(TABLE_NAME,column,null,null,null,
null,null);
Integernum=cursor.getCount();
共"
+Integer.toString(num)+"
条记录\n"
cursor.moveToFirst();
while(cursor.getPosition()!
=cursor.getCount()){
txtMsg.append(Integer.toString(cursor.getPosition())+"
+String.valueOf(cursor.getString(0))+"
+cursor.getString
(1)+"
+String.valueOf(cursor.getString
(2))+"
cursor.moveToNext();
读取数据失败\n"
*删除数据表
privatevoiddropTable(){
DROPTABLEIFEXISTS"
+TABLE_NAME;
数据删除出错!
2、ContentProvider(内容提供器)的Demo
LinearLayoutxmlns:
MyUser.java
import.Uri;
importandroid.provider.BaseColumns;
publicclassMyUser{
//必须要有_id字段。
本例中BaseColumn类中已经包含了_id字段
publicstaticfinalclassUserimplementsBaseColumns{
//定义CONTENT_URI
publicstaticfinalUriCONTENT_URI=Uri.parse("
content:
//com.webabcd.MyContentProvider"
//表数据列
publicstaticfinalStringUSER_NAME="
USER_NAME"
}
MyContentProvider.java
packagecom.lgl;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importorg.apache.http.util.EncodingUtils;
importandroid.content.ContentProvider;
importandroid.content.ContentUris;
importandroid.database.MatrixCursor;
//继承ContentProvider以实现自定义的ContentProvider(基于文件的信息存储)
publicclassMyContentProviderextendsContentProvider{
privateFilefile;
privateFileOutputStreamout;
privateFileInputStreamin;
//ContentProvider的删除数据接口
publicintdelete(Uriuri,Stringselection,String[]selectionArgs){
return0;
publicStringgetType(Uriuri){
returnnull;
//ContentProvider的插入数据接口
publicUriinsert(Uriuri,ContentValuesvalues){
out=newFileOutputStream(file);
out.write(values.getAsString(MyUser.User.USER_NAME).getBytes());
out.close();
introwId=0;
UrirowUri=ContentUris.appendId(MyUser.User.CONTENT_URI.buildUpon(),rowId).build();
getContext().getContentResolver().notifyChange(rowUri,null);
returnrowUri;
returnnull;
//创建用于保存信息的文件
publicbooleanonCreate(){
//每个包中应用程序的私有目录为:
/data/data/包名/
//SD卡目录为:
/sdcard
file=newFile("
/data/data/com.webabcd.contentprovider/"
demo.txt"
if(!
file.exists())
file.createNewFile();
returntrue;
}catch(Exceptionex){
returnfalse;
//