android中的内容提供者把自己数据库暴露出来
郑州app开发通过内容提供者把自己数据库暴露出来。布局代码主要是两个按钮,一个是查询按钮,一个是插入按钮,就不再这里展示了。
成都创新互联-专业网站定制、快速模板网站建设、高性价比扬州网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式扬州网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖扬州地区。费用合理售后完善,十载实体公司更值得信赖。
MainActivity.java
package cn.xhhkj.cyd;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.bundle;
import android.os.Ibinder;
import android.util.Log;
import android.view.View;
public class MainActivity extends AppCompatActivity {
private SQLiteDatabase database;
private static final String TAG = "结果是";
@Override
protected void onCreate(bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyOpenHelper helper = new MyOpenHelper(this);
database = helper.getReadableDatabase();
}
public void insert(View view) {
database.execSQL("insert into info(name,phone) values('王小明','13777777')");
database.execSQL("insert into info(name,phone) values('赵小明','13888888')");
database.execSQL("insert into info(name,phone) values('李大明','13666666')");
}
public void query(View view) {
Cursor cursor = database.rawQuery("select * from info", null);
while(cursor.moveToNext()){
String name = cursor.getString(cursor.getColumnIndex("name"));
String phone = cursor.getString(cursor.getColumnIndex("phone"));
System.out.println("name="+name+"phone="+phone);
Log.d(TAG,"name="+name+"phone="+phone);
}
cursor.close();
}
@Override
protected void onDestroy() {
super.onDestroy();
database.close();
}
}
MyOpenHelper.java
package cn.xhhkj.cyd;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class MyOpenHelper extends SQLiteOpenHelper {
public MyOpenHelper(Context context) {
super(context, "xhhkj.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table info(_id integer primary key autoincrement,name varchar(20),phone varchar(20))");
db.execSQL("alter table info add age integer");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
分享标题:android中的内容提供者把自己数据库暴露出来
本文地址:http://ybzwz.com/article/sccese.html