Android 每次运行程序时,记录都会插入数据库。SQLITE数据库 //以下是内容提供商的代码。它有两个uri//1)content://authority/student(表名)和//2)content://authority/student/_id 识别每个学生的身份。 公共类StudentProvider扩展了ContentProvider{ 公共静态最终整数学生=0; 公共静态最终int学生ID=1; 私有静态HashMap学生投影图; 公立学生助教; UriMatcher match=buildUrimatcher(); 静态UriMatcher buildUrimatcher() { UriMatcher matcher=新的UriMatcher(UriMatcher.NO_匹配); addURI(StudentContract.CONTENT\u AUTHORITY,StudentContract.PATH,STUDENT); matcher.addURI(StudentContract.CONTENT\u AUTHORITY,StudentContract.PATH+“/”,STUDENT\u ID); 返回匹配器; } @凌驾 公共布尔onCreate(){ helper=newstudenthelper(getContext()); 返回true; } @凌驾 公共游标查询(Uri Uri、字符串[]字符串、字符串s、字符串[]字符串s2、字符串s2){ SQLiteDatabase=helper.getReadableDatabase(); SQLiteQueryBuilder=新SQLiteQueryBuilder(); builder.setTables(StudentContract.Student.Table); intid=match.match(uri); 开关(id) { 个案学生: { setProjectionMap(学生投影地图); 打破 } 个案学生编号: { builder.appendWhere(StudentContract.Student._ID+“=”+uri.getPathSegments().get(1)); 打破 } 违约: 抛出新的IllegalArgumentException(“未知URI”+URI); } Cursor Cursor=builder.query(数据库、字符串、s、字符串2、null、null、s2); cursor.setNotificationUri(getContext().getContentResolver(),uri); 返回光标; } @凌驾 public int bulkInsert(Uri,ContentValues[]值){ SQLiteDatabase=helper.getWritableDatabase(); intid=match.match(uri); 开关(id) { 个案学生: { database.beginTransaction(); 整数计数=0; 尝试 { for(ContentValues值:值) { long rid=database.insert(StudentContract.Student.Table,null,value); 如果(rid!=-1) { 计数++; 日志d(“批量插入”、“成功”); } } database.setTransactionSuccessful(); }最后{ 数据库。endTransaction(); } getContext().getContentResolver().notifyChange(uri,null); 返回计数; } 默认值:返回super.bulkInsert(uri,值); } } } //下面是将记录从//数组插入数据库的活动代码。Im使用批量插入方法将记录插入数据库。
//用于将记录插入数据库的代码Android 每次运行程序时,记录都会插入数据库。SQLITE数据库 //以下是内容提供商的代码。它有两个uri//1)content://authority/student(表名)和//2)content://authority/student/_id 识别每个学生的身份。 公共类StudentProvider扩展了ContentProvider{ 公共静态最终整数学生=0; 公共静态最终int学生ID=1; 私有静态HashMap学生投影图; 公立学生助教; UriMatcher match=buildUrimatcher(); 静态UriMatcher buildUrimatcher() { UriMatcher matcher=新的UriMatcher(UriMatcher.NO_匹配); addURI(StudentContract.CONTENT\u AUTHORITY,StudentContract.PATH,STUDENT); matcher.addURI(StudentContract.CONTENT\u AUTHORITY,StudentContract.PATH+“/”,STUDENT\u ID); 返回匹配器; } @凌驾 公共布尔onCreate(){ helper=newstudenthelper(getContext()); 返回true; } @凌驾 公共游标查询(Uri Uri、字符串[]字符串、字符串s、字符串[]字符串s2、字符串s2){ SQLiteDatabase=helper.getReadableDatabase(); SQLiteQueryBuilder=新SQLiteQueryBuilder(); builder.setTables(StudentContract.Student.Table); intid=match.match(uri); 开关(id) { 个案学生: { setProjectionMap(学生投影地图); 打破 } 个案学生编号: { builder.appendWhere(StudentContract.Student._ID+“=”+uri.getPathSegments().get(1)); 打破 } 违约: 抛出新的IllegalArgumentException(“未知URI”+URI); } Cursor Cursor=builder.query(数据库、字符串、s、字符串2、null、null、s2); cursor.setNotificationUri(getContext().getContentResolver(),uri); 返回光标; } @凌驾 public int bulkInsert(Uri,ContentValues[]值){ SQLiteDatabase=helper.getWritableDatabase(); intid=match.match(uri); 开关(id) { 个案学生: { database.beginTransaction(); 整数计数=0; 尝试 { for(ContentValues值:值) { long rid=database.insert(StudentContract.Student.Table,null,value); 如果(rid!=-1) { 计数++; 日志d(“批量插入”、“成功”); } } database.setTransactionSuccessful(); }最后{ 数据库。endTransaction(); } getContext().getContentResolver().notifyChange(uri,null); 返回计数; } 默认值:返回super.bulkInsert(uri,值); } } } //下面是将记录从//数组插入数据库的活动代码。Im使用批量插入方法将记录插入数据库。,android,sqlite,Android,Sqlite,//用于将记录插入数据库的代码 //Here is the code for Content provider. It has two uri's //1)content://authority/student(table name) and //2)content://authority/student/_id to identify each student. public class StudentProvider extends ContentProvider { publi
//Here is the code for Content provider. It has two uri's //1)content://authority/student(table name) and //2)content://authority/student/_id to identify each student.
public class StudentProvider extends ContentProvider {
public static final int STUDENT=0;
public static final int STUDENT_ID=1;
private static HashMap<String, String> STUDENTS_PROJECTION_MAP;
public StudentHelper helper;
UriMatcher match=buildUrimatcher();
static UriMatcher buildUrimatcher()
{
UriMatcher matcher=new UriMatcher(UriMatcher.NO_MATCH);
matcher.addURI(StudentContract.CONTENT_AUTHORITY,StudentContract.PATH,STUDENT);
matcher.addURI(StudentContract.CONTENT_AUTHORITY,StudentContract.PATH+"/#",STUDENT_ID);
return matcher;
}
@Override
public boolean onCreate() {
helper=new StudentHelper(getContext());
return true;
}
@Override
public Cursor query(Uri uri, String[] strings, String s, String[] strings2, String s2) {
SQLiteDatabase database=helper.getReadableDatabase();
SQLiteQueryBuilder builder=new SQLiteQueryBuilder();
builder.setTables(StudentContract.Student.Table);
int id=match.match(uri);
switch (id)
{
case STUDENT:
{
builder.setProjectionMap(STUDENTS_PROJECTION_MAP);
break;
}
case STUDENT_ID:
{
builder.appendWhere(StudentContract.Student._ID+"="+uri.getPathSegments().get(1));
break;
}
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
Cursor cursor=builder.query(database,strings,s,strings2,null,null,s2);
cursor.setNotificationUri(getContext().getContentResolver(),uri);
return cursor;
}
@Override
public int bulkInsert(Uri uri, ContentValues[] values) {
SQLiteDatabase database=helper.getWritableDatabase();
int id=match.match(uri);
switch (id)
{
case STUDENT:
{
database.beginTransaction();
int count=0;
try
{
for(ContentValues value:values)
{
long rid= database.insert(StudentContract.Student.Table,null,value);
if(rid!=-1)
{
count++;
Log.d("Bulk insert","success");
}
}
database.setTransactionSuccessful();
}finally {
database.endTransaction();
}
getContext().getContentResolver().notifyChange(uri,null);
return count;
}
default:return super.bulkInsert(uri, values);
}
}
}
//Here is the code of Activity which inserts records into database from an //array. Im inserting records into database using bulk insert method.
final View rootView=充气机。充气(R.layout.fragment_main,container,false);
字符串[]名称=新字符串[]{“sai”、“kiran”、“seenu”、“akhil”、“devi”、“sanath”、“patro”、“patch”、“reddy”、“sai kiran”};
ListView ListView=(ListView)rootView.findViewById(R.id.list\u视图);
setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
公共无效onItemClick(AdapterView AdapterView、View视图、int i、long l){
游标游标=(游标)adapterView.getItemAtPosition(i);
Uri Student=ContentUris.withAppendedId(StudentContract.Student.CONTENT\u Uri,cursor.getInt(cursor.getColumnIndex(StudentContract.Student.ID));
Toast.makeText(getActivity(),Student.toString(),Toast.LENGTH_SHORT).show();
}
});
游标c=getActivity().getContentResolver().query(StudentContract.Student.CONTENT\u URI,null,null,null);
cursorAdapter=newtodocursoradapter(getActivity(),c,0);
字符串[]等级=新字符串[]{“A”、“A1”、“B”、“C”、“A1”、“C”、“D”、“D”、“B”、“A”};
向量值向量=新向量(名称.长度);
对于(int i=0;i0)
{
ContentValues[]values=新的ContentValues[valuesVector.size()];
valuesVector.toArray(valueses);
getActivity().getContentResolver().bulkInsert(StudentContract.Student.CONTENT\u URI,values);
}
setAdapter(游标适配器);
每次执行该程序时,都会将10条记录再次插入数据库。我不希望每次执行程序时都插入记录。我是这个领域的新手,任何帮助都将不胜感激。
StudentHelper类扩展了Sqliteopenhelper,它包含oncreate()和onupgrade()方法为什么会出现这种情况
final View rootView = inflater.inflate(R.layout.fragment_main, container, false);
String[] names=new String[]{"sai","kiran","seenu","akhil","devi","sanath","patro","patch","reddy","sai kiran"};
ListView listView= (ListView) rootView.findViewById(R.id.list_view);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Cursor cursor= (Cursor) adapterView.getItemAtPosition(i);
Uri Student= ContentUris.withAppendedId(StudentContract.Student.CONTENT_URI,cursor.getInt(cursor.getColumnIndex(StudentContract.Student._ID)));
Toast.makeText(getActivity(),Student.toString(),Toast.LENGTH_SHORT).show();
}
});
Cursor c=getActivity().getContentResolver().query(StudentContract.Student.CONTENT_URI,null,null,null,null);
cursorAdapter =new TodoCursorAdapter(getActivity(),c,0);
String[] grades=new String[]{"A","A1","B","C","A1","C","D","D","B","A"};
Vector<ContentValues> valuesVector=new Vector<ContentValues>(names.length);
for(int i=0;i<10;i++)
{
ContentValues values=new ContentValues();
values.put(StudentContract.Student.name,names[i]);
values.put(StudentContract.Student.grade,grades[i]);
valuesVector.add(values);
}
if(valuesVector.size()>0)
{
ContentValues[] valueses=new ContentValues[valuesVector.size()];
valuesVector.toArray(valueses);
getActivity().getContentResolver().bulkInsert(StudentContract.Student.CONTENT_URI,valueses);
}
listView.setAdapter(cursorAdapter);
for(int i=0;i
for(int i=0;i<10;i++) {
ContentValues values=new ContentValues();
values.put(StudentContract.Student.name,names[i]);
values.put(StudentContract.Student.grade,grades[i]);
valuesVector.add(values);
}