25/9/8/SQLite/ContentProvider

来源:互联网 发布:nexus9跳过网络验证 编辑:程序博客网 时间:2024/05/16 07:09

SQLite

ContentProvider

Android是对SQLite3进行操作的,其操作步骤一般是:

1.首先建立一个Java类继承于SQLiteOpenHelper重写里面的方法
2.在onCreate方法中用sql语句创建一个表
3.在MainActivity中声明这个类,然后调用getWritableDatabase()方法生成一个SQLiteDatabase对象,然后利用这个对象对创建的表进行增删改查的操作
4.代码实现

public class SqliteOpenHelperTest extends SQLiteOpenHelper {    public SqliteOpenHelperTest(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {        super(context, name, factory, version);    }    public SqliteOpenHelperTest(Context context,String name){        this(context,name,null,1);    }    @Override    public void onCreate(SQLiteDatabase db) {        db.execSQL("create table if not exists user (id integer primary key autoincrement ,name varchar(20),password varchar(20)) ");    }    @Override    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {    }} private SQLiteDatabase db;  SqliteOpenHelperTest sqlite=new SqliteOpenHelperTest(getApplicationContext(),"SQL_FIRST_DB.db");        db=sqlite.getWritableDatabase();case R.id.sql_button_insert:                ContentValues value=new ContentValues();                value.put("name",mEditTextUser.getText().toString());                value.put("password",mEditTextPassword.getText().toString());                db.insert("user",null,value);                break;            case R.id.sql_button_updata:                ContentValues value1=new ContentValues();                value1.put("password","0123456");                db.update("user",value1,"name=?",new String[]{"Jinx"});                break;            case R.id.sql_button_delete:                db.delete("user","name=?",new String[]{"Jinx"});                break;            case R.id.sql_button_query://                Cursor cursor=db.rawQuery("select * from user", null);                Cursor cursor=db.query("user",null,null,null,null,null,"id DESC","2,4");//2和4 分别表示偏移量和数量DESC表示倒序的方式排列                cursor.moveToFirst();                while (!cursor.isAfterLast()){                    String name=cursor.getString(cursor.getColumnIndex("name"));                    String password=cursor.getString(cursor.getColumnIndex("password"));                    Log.d("cursor","用户名为:"+name+"密码为:"+password);                    cursor.moveToNext();                }                break;

ContentProvider主要用于在不同的应用程序之间实现数据共享的功能,它的基本用法是:

 private ArrayAdapter<String> mAdapter;    List<String> mContactsList=new ArrayList<>();    mListView= (ListView) findViewById(R.id.contacts_listview);        mAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,mContactsList);        mListView.setAdapter(mAdapter);        readContacts();private void readContacts() {        Cursor cursor=null;        cursor=getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,null,null,null);        while (cursor.moveToNext()){            String displyName=cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));            String number=cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));            mContactsList.add(displyName+"\n"+number);        }    }
0 0