无法在android中的SQLite数据库中插入行

时间:2018-04-11 14:00:37

标签: android sqlite

我试图在SQLite数据库的表中插入几行,我将这部分代码包含在主活动类中,如下所示:

using (SqlConnection cn = new SqlConnection(_connectionString))
{
    cn.Open();
    var predicate = Predicates.Field<SomeTable>(f => f.Id, Operator.???, commaSeparatedListOfIDs);
    IEnumerable<SomeTable> list = cn.GetList<SomeTable>(predicate);
    cn.Close();
}

我使用自定义列表视图来检索学校名称,但是我在运行时遇到错误并且没有添加数据,以下是数据库类:

Operator.In

LOG:

public class MainActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener {

    private TextView mTextMessage;

    Intent intent1;

    private DrawerLayout d;
    private ActionBarDrawerToggle a;

    TabLayout tab;

    ViewPager vp;

    ViewPagerAdapter vpa;

    GridLayout mainGrid;

    DBAdapter db;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        insertSchools();

        d = (DrawerLayout) findViewById(R.id.container);
        a= new ActionBarDrawerToggle(MainActivity.this,d,R.string.open,R.string.close);



        d.addDrawerListener(a);
        a.syncState();

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        intent1= new Intent(getApplicationContext(),MapsActivity.class);

       //NavigationView view = (NavigationView) findViewById(R.id.n);

        mainGrid = (GridLayout) findViewById(R.id.mainGrid);

        //Set Event
        setSingleEvent(mainGrid);
        //setToggleEvent(mainGrid);

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(a.onOptionsItemSelected(item)){

                   return  true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {

        int id = item.getItemId();

        if(id == R.id.item1){

            startActivity(intent1);

        }

        if(id==R.id.item2){

            Toast.makeText(this,"رأيك يهمنا",Toast.LENGTH_SHORT).show();
        }




        if(id==R.id.item5){

            Toast.makeText(this,"حول التطبيق",Toast.LENGTH_SHORT).show();
        }


        return false;
    }


    private void setToggleEvent(GridLayout mainGrid) {
        //Loop all child item of Main Grid
        for (int i = 0; i < mainGrid.getChildCount(); i++) {
            //You can see , all child item is CardView , so we just cast object to CardView
            final CardView cardView = (CardView) mainGrid.getChildAt(i);


            cardView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (cardView.getCardBackgroundColor().getDefaultColor() == -1) {
                        //Change background color
                        cardView.setCardBackgroundColor(Color.parseColor("#FF6F00"));
                        Toast.makeText(MainActivity.this, "State : True", Toast.LENGTH_SHORT).show();

                    } else {
                        //Change background color
                        cardView.setCardBackgroundColor(Color.parseColor("#FFFFFF"));
                        Toast.makeText(MainActivity.this, "State : False", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    }

    private void setSingleEvent(GridLayout mainGrid) {
        //Loop all child item of Main Grid
        for (int i = 0; i < mainGrid.getChildCount(); i++) {
            //You can see , all child item is CardView , so we just cast object to CardView
            CardView cardView = (CardView) mainGrid.getChildAt(i);
            final int finalI = i;
            cardView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    if(finalI==0) {
                        Intent intent = new Intent(MainActivity.this, ListOfSchoolsPublic.class);
                        startActivity(intent);
                    }

                    else if(finalI==1){

                        Intent intent = new Intent(MainActivity.this,ListOfPrivateSchools.class);
                        startActivity(intent);

                    }

                }
            });
        }
    }


    /////////////////////////////INSERT SCHOOLS//////////////////////////

    public  void insertSchools(){

        //SCHOOL-1
        openDB();
        db.insertSchool("مدرسة دار المعرفة","0797417018 - 0790889165","Math - Science",2009,"الأول - السابع","عن طريق مستودع التربية","جيدة جدا","مختبر حاسوب - مختبر علوم","480 - 550","اربد/مجمع الشيخ خليل/حي النزهة",35.85542,"خاصة",32.54845);
        closeDB();
        //SCHOOL-2
        openDB();
        db.insertSchool("مدرسة الروم الكاثوليك","07272881","فرنسي",1951,"بستان - ثاني ثانوي","الشراء من مستودع التربية","ممتازة","مختبر حاسوب - مختبر علوم - مكتبة - فرقة فن","روضة / 500 | الاول - ثاني ثنوي / 1100","اربد/شارع الحصن/بجانب اسواق مكة",35.85155,"خاصة",32.55172);
        closeDB();
        //SCHOOL-3
        openDB();
        db.insertSchool("مدرسة النوار بنت مالك ","لا يوجد","لا يوجد",1970,"لا يوجد","لا يوجد","لا يوجد","لا يوجد","لا يوجد","لا يوجد",35.85391,"حكومية",32.54843);
        closeDB();
        //SCHOOL-4
        openDB();
        db.insertSchool("مدرسة زبيدة بنت جعفر الأساسية","لا يوجد","لا يوجد",1970,"الثالث - السابع","لا يوجد","جيدة","لا يوجد","لا يوجد","لا يوجد",35.85316,"خاصة",32.54871);
        closeDB();
        //SCHOOL-5
        openDB();
        db.insertSchool("أكاديمية الفرح","027250551 - 0796550752","الغاز عربية للصف الاول - كراسة كتابة وخط - مصطلحات علمية باللغة الانجليزية - Math - Science - منهاج تعليمي الكتروني للرياضيات والعلوم والانجليزي من الصف الخامس للصف التاسع",1994,"الروضة - ثاني ثانوي","لا يوجد","لا يوجد","مختبر حاسوب","اول - سادس / 600 | سابع - تاسع / 700 | عاشر / 750 | اول ثانوي - ثاني ثانوي / 900 | الروضة / 450","اربد/الحي الجنوبي/شمال جامعة اليرموك/جنوب شرق اشارة الاسكان",35.85199,"خاصة",32.54356);
        closeDB();
        //SCHOOL-6
        openDB();
        db.insertSchool("مدرسة جمانة الثانوية للبنات","لا يوجد","لا يوجد",1970,"لا يوجد","لا يوجد","لا يوجد","لا يوجد","لا يوجد","لا يوجد",35.85352,"خاصة",32.53322);
        closeDB();

        //SCHOOL-7
        openDB();
        db.insertSchool("روضة ومدرسة ابن النفيس","لا يوجد","لا يوجد",1970,"لا يوجد","لا يوجد","لا يوجد","لا يوجد","لا يوجد","لا يوجد",35.85050,"حكومية",32.54680);
        closeDB();

        //SCHOOL-8
        openDB();
        db.insertSchool("مدرسة البحرينية","0772062317","لا يوجد",1970,"الأول - العاشر","لا يوجد","جيدة جدا","مختبر علوم - مختبر حاسوب1 - مختبر حاسوب2 - حضانة - حديقة","لا يوجد","اربد/الحي الشرقي/النزهة",35.86408,"حكومية",32.54847);
        closeDB();

        //SCHOOL-9
        openDB();
        db.insertSchool("مدرسة القادسية","0795527721","لا يوجد",1981,"لا يوجد","لا يوجد","ممتازة","مختبر حاسوب - مختبر علوم","لا يوجد","اربد/الحي الشرقي",35.86587,"حكومية",32.54785);
        closeDB();

        //SCHOOL-10
        openDB();
        db.insertSchool("مدرسة رقية بنت الرسول الأساسية للبنات","لا يوجد","لا يوجد",1970,"لا يوجد","لا يوجد","لا يوجد","لا يوجد","لا يوجد","لا يوجد",35.85827,"خاصة",32.55319);
        closeDB();

        //SCHOOL-11
        openDB();
        db.insertSchool("مدرسة الإيمان التعليمية","07277323","Math - Science",1996,"الروضة - التاسع","الشراء من مستودع التربية","ممتازة","لا يوجد","الروضة / 850 | الاول - التاسع /1000","اربد/حي الشيخ خليل/مبنى راتب البطاينة",35.85887,"حكومية",32.54622);
        closeDB();



    }



    /////////////////////////////////////////////////////////////////////





    @Override
    protected void onDestroy() {
        super.onDestroy();
        closeDB();

    }// end onDestroy method



    //////////////////////////////////////////////////////////////////////



    private void openDB() {
        db = new DBAdapter(this);
        db.open();
    } // end openDB method

    //////////////////////////////////////////////////////////////////////

    private void closeDB() {
        db.close();
    } // end closeDB method

}

我不知道为什么行没有插入数据库,请帮忙!?!阿拉伯语是什么原因?

2 个答案:

答案 0 :(得分:0)

我认为这是数据类型错误。 fDate是整数,我认为ContentValues需要键和值String。

initialValues.put(KEY_FOUNDATION_DATE, ""+fDate);

答案 1 :(得分:0)

在没有实际测试代码的情况下,它看起来像你只是有一个小错误导致错误。您使用列名schools(定义为KEY_SCHOOLNAME)创建了schoolName表,但在insertSchool方法中,您似乎拥有initialValues.put(KEY_NAME, name);,其使用的是name users表中的列名insertSchool。我想如果您只是将initialValues.put(KEY_SCHOOLNAME, name);中的那一行更改为private void setUpCameraOutputs(int width, int height){ Activity activity = getActivity(); CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE); try { for (String cameraId : manager.getCameraIdList()) { CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId); //Check if autofocus is available on camera int[] afAvailableModes = characteristics.get(CameraCharacteristics.CONTROL_AF_AVAILABLE_MODES); if (afAvailableModes.length == 0 || (afAvailableModes.length == 1 && afAvailableModes[0] == CameraMetadata.CONTROL_AF_MODE_OFF)) { mAutoFocusSupported = false; } else { mAutoFocusSupported = true; } //end autofocus check //additional camera setup code here that is not relevant to this problem } private void takePicture() { if (mAutoFocusSupported) { lockFocus(); } else { captureStillPicture(); } } private void lockFocus() { try { // This is how to tell the camera to lock focus. mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_START); // Tell #mCaptureCallback to wait for the lock. mState = STATE_WAITING_LOCK; mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback, mBackgroundHandler); } catch (CameraAccessException e) { e.printStackTrace(); } } private void captureStillPicture() { try { final Activity activity = getActivity(); if (null == activity || null == mCameraDevice) { return; } // This is the CaptureRequest.Builder that we use to take a picture. final CaptureRequest.Builder captureBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE); captureBuilder.addTarget(mImageReader.getSurface()); // Use the same AE and AF modes as the preview. captureBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE); //setFlashMode(captureBuilder); setFlashOn(captureBuilder); // Orientation int captureRotation = activity.getWindowManager().getDefaultDisplay().getRotation(); captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, getOrientation(captureRotation)); CameraCaptureSession.CaptureCallback CaptureCallback = new CameraCaptureSession.CaptureCallback() { @Override public void onCaptureCompleted(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull TotalCaptureResult result) { unlockFocus(); synchronized(lock) { captureComplete(captureRotation); } } }; mCaptureSession.stopRepeating(); mCaptureSession.abortCaptures(); mCaptureSession.capture(captureBuilder.build(), CaptureCallback, null); } catch (CameraAccessException e) { e.printStackTrace(); } } ,那么它将解决您的错误。