每次运行程序时,记录都会插入到数据库中。 SQLitedatabase

时间:2016-07-14 09:14:24

标签: 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 {
    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 = 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);

每次执行程序时,都会再将10条记录插入数据库。我不希望每次执行程序时都插入记录。我是这个领域的初学者,任何帮助表示赞赏。 StudentHelper类扩展了Sqliteopenhelper,它包含oncreate()和onupgrade()方法

1 个答案:

答案 0 :(得分:0)

为什么会这样?

  <select ng-model="a" 
          ng-options="detail.name for detail in details | filter:{shortDescription:{$:''}} track by detail.name">
  </select>