android_获取另一个数据库的内容

在数据库的项目中新建一个类:

package com.zking.administrator.g160618_android16_sqlite;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;

import com.zking.db.DBHelper;

/**
 * Created by Administrator on 2017/7/17 0017.
 */

public class MyProvider extends ContentProvider {
    @Override
    public boolean onCreate() {
        Log.i("test","onCreate");
        return false;
    }

    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
        Log.i("test","query");

        //把数据查询出来
        DBHelper dbHelper=new DBHelper(getContext(),"G160618.db",null,2);
        SQLiteDatabase sqLiteDatabase=dbHelper.getReadableDatabase();
        //sqLiteDatabase.rawQuery("select * from login",null);
        return sqLiteDatabase.rawQuery("select * from login",null);
    }

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {
        Log.i("test","getType");
        return null;
    }

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
        Log.i("test","insert");
        return null;
    }

    @Override
    public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
        Log.i("test","delete");
        return 0;
    }

    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
        Log.i("test","update");
        return 0;
    }
}
项目配置:

<!--配置内容提供者-->
<provider
    android:authorities="com.zking.administrator.g160618_android16_sqlite.bte"
    android:name=".MyProvider"
    android:exported="true"
    ></provider>
想要获取的数据库的项目:

ContentResolver contentProvider=getContentResolver();
Uri uri=Uri.parse("content://com.zking.administrator.g160618_android16_sqlite.bte");
Cursor cursor=contentProvider.query(uri,null,null,null,null);
while(cursor.moveToNext()){
//通过列名获取数据库数据
   int uid=cursor.getInt(cursor.getColumnIndex("uid"));
    String uname=cursor.getString(cursor.getColumnIndex("uname")) ;
    String upass=cursor.getString(cursor.getColumnIndex("upass")) ;
    Log.i("test",uid+" "+uname+" "+upass);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值