如果不存在,将新数据插入sqlite

时间:2014-05-04 13:49:13

标签: android sqlite

我需要弄清楚是否有可能,如何在第一次打开课程时自动插入年份。在实践中,如果班级没有找到它是什么,2014年已成为2014年列的一部分,如果它存在,请不要输入。 我的数据库因此创建: @

Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_TABLE="CREATE TABLE "+TURNI+" ("+TURNI_ID+" INTEGER PRIMARY KEY,"+DATA+" NUMERIC,"+MESE+" TEXT,"+ANNO+" NUMERIC)";
        db.execSQL(CREATE_TABLE);

如果没有,我需要在使用的变化中插入年份     @覆盖     public void onCreate(Bundle savedInstanceState){         super.onCreate(savedInstanceState);         的setContentView(R.layout.display_turni);

    turniList = (ListView) findViewById(R.id.List);

    mHelper = new DbHelper(this);

    //aggiunta nuovi record
    findViewById(R.id.anno_txt).setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

            Intent i = new Intent(getApplicationContext(),
                    Set_anno.class);
            String mese="Gennaio  ";
            String Mese=getPackageName();
               i.putExtra(Mese+".Intmese", mese);
            i.putExtra("update", false);
            startActivity(i);

        }
    });

    //click aggiorna dati
    turniList.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {


            Intent i = new Intent(getApplicationContext(),
                    Aggiungi_turni.class);
            SharedPreferences prefs = getSharedPreferences(MY_PREFERENCES, Context.MODE_PRIVATE);
            Calendar calendar = Calendar.getInstance();
            int year = calendar.get(Calendar.YEAR);
            String anno = prefs.getString(ANNO, Integer.toString(year));

            String mese="Gennaio  ";
            String Mese=getPackageName();
            i.putExtra(Mese+".Intmese", mese);
            i.putExtra("Anno", anno);
            i.putExtra("Mese", turni_mese.get(arg2));
            i.putExtra("Turno", turni_turno.get(arg2));
            i.putExtra("Ore", turni_ore.get(arg2));
            i.putExtra("Notes", turni_notes.get(arg2));
            i.putExtra("ID", turni_Id.get(arg2));
            i.putExtra("update", true);
            startActivity(i);

        }
    });

    //click lungo cancellazione record
    turniList.setOnItemLongClickListener(new OnItemLongClickListener() {

        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                final int arg2, long arg3) {

            build = new AlertDialog.Builder(Gennaio.this);
            build.setTitle("Cancella " + turni_mese.get(arg2)+ " "
                    + turni_turno.get(arg2)+ " "
                    + turni_ore.get(arg2)+ ""+
                    turni_notes.get(arg2));
            build.setMessage("Vuoi cancellarlo ?");
            build.setPositiveButton("Si",
                    new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog,
                                int which) {

                            Toast.makeText(
                                    getApplicationContext(),
                                    turni_mese.get(arg2)
                                            + turni_turno.get(arg2)
                                            + turni_ore.get(arg2)
                                            + turni_notes.get(arg2)
                                            + "è stato cancellato.", 3000).show();

                            dataBase.delete(
                                    DbHelper.TURNI,
                                    DbHelper.TURNI_ID + "="
                                            + turni_Id.get(arg2), null);
                            displayData();
                            dialog.cancel();
                        }
                    });

            build.setNegativeButton("No",
                    new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog,
                                int which) {
                            dialog.cancel();
                        }
                    });
            AlertDialog alert = build.create();
            alert.show();

            return true;
        }
    });
    }


@Override
    public void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.display_tutti);

        tuttiList = (ListView) findViewById(R.id.List);

        mHelper = new DbHelper(this);

        //aggiunta nuovi record
        findViewById(R.id.anno_txt).setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                Intent i = new Intent(getApplicationContext(),
                        Set_anno.class);
                String mese="Gennaio  ";
                String Mese=getPackageName();
                   i.putExtra(Mese+".Intmese", mese);
                i.putExtra("update", false);
                startActivity(i);

            }
        });

        //click aggiorna dati
        tuttiList.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {


                Intent i = new Intent(getApplicationContext(),
                        Aggiungi_turni.class);
                SharedPreferences prefs = getSharedPreferences(MY_PREFERENCES, Context.MODE_PRIVATE);
                Calendar calendar = Calendar.getInstance();
                int year = calendar.get(Calendar.YEAR);
                String anno = prefs.getString(ANNO, Integer.toString(year));

                String mese="Gennaio  ";
                String Mese=getPackageName();
                i.putExtra(Mese+".Intmese", mese);
                i.putExtra("Anno", anno);
                i.putExtra("Mese", giorni_mese.get(arg2));
                i.putExtra("ID", giorni_Id.get(arg2));
                i.putExtra("update", true);
                startActivity(i);

            }
        });


        }


    @Override
    protected void onResume() {
        displayData();
        super.onResume();
    }

    /**
     * Visualizza dati da SQLite
     */
    private void displayData() {
        SharedPreferences prefs = getSharedPreferences(MY_PREFERENCES, Context.MODE_PRIVATE);
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        String anno = prefs.getString(ANNO, Integer.toString(year));

        TextView textanni = (TextView) findViewById(R.id.anno_txt);
        textanni.setText("Gennaio  "+anno);
        int anni = Integer.parseInt(anno);      

        dataBase = mHelper.getWritableDatabase();
        Calendar mycal = new GregorianCalendar(anni, Calendar.JANUARY, 1);
        int giorni = mycal.getActualMaximum(Calendar.DAY_OF_MONTH);
        int dayOfWeak = mycal.get(Calendar.DAY_OF_WEEK);

        switch (dayOfWeak) {
            case Calendar.SUNDAY:
                break;
            case Calendar.MONDAY:
                break;
            case Calendar.TUESDAY:
                break;
            case Calendar.WEDNESDAY:
                break;
            case Calendar.THURSDAY:
                break;
            case Calendar.FRIDAY:
                break;
            case Calendar.SATURDAY:
                break;     

        }   

1 个答案:

答案 0 :(得分:0)

作为预检,如果您需要存储的只是一个值(没有实体结构),我宁愿使用SharedPreferences来解决这个问题。

我担心在两种情况下,SharedPreferences和SQLite,你需要在知道它是否存在之前检索一个键或表的值。如果它有帮助,请注意SQLiteDatabase中的insertWithOnConflict()和update()/ updateWithOnConflict方法返回新插入或现有行id的id或-1,以防错误,并分别更新行数。这可以帮助您了解您的插入或更新是否对您的数据模型有任何影响而不执行其他请求。