2013-12-09 73 views
0

在我的Android應用程序,我想在sqloite中做一個數據庫..我能夠爲VARCHAR創建...我沒有代碼創建名字,中間名和姓氏的數據庫..它工作..但是當我添加日期... DOB ..應用程序崩潰...我給VARCHAR的名稱...並給NUMERIC日期...我認爲這個數字可能是錯誤的......但我試圖給整數,文本但isalo崩潰。我會給代碼..請檢查,如果有任何錯誤..pls hgelpAndroid的Sqlite日期錯誤

mainActivity

{ 

    SQLiteDatabase db; 
    String fname,mname,lname,cfname,cmname,clname,bday,bmonth,byear,ett1,ett2,ett3,ett4,ett5,ett6,ett7,ett8,ett9; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 

    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.fullexplanationentry_xm); 

     db = openOrCreateDatabase("MyDBI", MODE_PRIVATE, null); 


     db.execSQL("CREATE TABLE IF NOT EXISTS DATA (fname VARCHAR, mname VARCHAR,lname VARCHAR,cfname VARCHAR, cmname VARCHAR,clname VARCHAR,bday NUMERIC,bmonth NUMERIC,byear NUMERIC);"); 


    } 

    public void save(View V) 
    { 
     EditText et1 = (EditText) findViewById (R.id.editText1); 
     EditText et2 = (EditText) findViewById (R.id.editText2); 
     EditText et3 = (EditText) findViewById (R.id.editText3); 
     EditText et4 = (EditText) findViewById (R.id.editText4); 
     EditText et5 = (EditText) findViewById (R.id.editText5); 
     EditText et6 = (EditText) findViewById (R.id.editText6); 
     EditText et7 = (EditText) findViewById (R.id.editText7); 
     EditText et8 = (EditText) findViewById (R.id.editText8); 
     EditText et9 = (EditText) findViewById (R.id.editText9); 



         fname = et1.getText().toString(); 

         mname = et2.getText().toString(); 

         lname = et3.getText().toString(); 

         cfname = et4.getText().toString(); 

         cmname = et5.getText().toString(); 

         clname = et6.getText().toString(); 

         bday = et7.getText().toString(); 

         bmonth = et8.getText().toString(); 

         byear = et9.getText().toString(); 


         db.execSQL("INSERT INTO Data VALUES('"+fname+"', '"+mname+"','"+lname+"','"+cfname+"', '"+cmname+"','"+clname+"','"+bday+"', '"+bmonth+"','"+byear+"');"); 
    } 

    public void saveddata(View V) 
    { 
     Cursor c = db.rawQuery("SELECT * from Data", null); 
     int count = c.getCount(); 
     c.moveToFirst(); 




     TableLayout tableLayout = new TableLayout(getApplicationContext()); 
     tableLayout.setVerticalScrollBarEnabled(true); 
     TableRow tableRow; 
     TextView textView1, textView2,textView3,textView4,textView5,textView6,textView7,textView8,textView9,textView10,textView11; 
     tableRow = new TableRow(getApplicationContext());   
     textView1 = new TextView(getApplicationContext()); 
     textView1.setText("FirstName"); 
     textView1.setTextColor(Color.RED); 
     textView1.setTypeface(null, Typeface.BOLD); 
     textView1.setPadding(10, 10, 10, 10); 
     tableRow.addView(textView1); 

     textView2 = new TextView(getApplicationContext()); 
     textView2.setText("LastName"); 
     textView2.setTextColor(Color.RED); 
     textView2.setTypeface(null, Typeface.BOLD); 
     textView2.setPadding(10, 10, 10, 10); 
     tableRow.addView(textView2); 

     tableLayout.addView(tableRow); 

     for(Integer j=0; j<count; j++) 


     { 


      tableRow = new TableRow(getApplicationContext()); 
      textView3= new TextView(getApplicationContext()); 
      textView3.setText(c.getString(c.getColumnIndex("fname"))); 
      textView3.setClickable(true); 

      ett1=textView3.getText().toString(); 
      //datas11=textView3.getText().toString(); 

      textView4 = new TextView(getApplicationContext()); 
      textView4.setText(c.getString(c.getColumnIndex("mname"))); 
      textView4.setClickable(true); 
      ett2=textView4.getText().toString(); 
      //datas12=textView4.getText().toString(); 

      textView5 = new TextView(getApplicationContext()); 
      textView5.setText(c.getString(c.getColumnIndex("lname"))); 
      textView5.setClickable(true); 
      ett3=textView5.getText().toString(); 
      //datas13=textView7.getText().toString(); 

      textView6 = new TextView(getApplicationContext()); 
      textView6.setText(c.getString(c.getColumnIndex("cfname"))); 
      textView6.setClickable(true); 
      ett4=textView6.getText().toString(); 

      textView7 = new TextView(getApplicationContext()); 
      textView7.setText(c.getString(c.getColumnIndex("cmname"))); 
      textView7.setClickable(true); 
      ett5=textView7.getText().toString(); 

      textView8 = new TextView(getApplicationContext()); 
      textView8.setText(c.getString(c.getColumnIndex("clname"))); 
      textView8.setClickable(true); 
      ett6=textView8.getText().toString(); 

      textView9 = new TextView(getApplicationContext()); 
      textView9.setText(c.getString(c.getColumnIndex("bday"))); 
      textView9.setClickable(true); 
      ett7=textView9.getText().toString(); 

      textView10 = new TextView(getApplicationContext()); 
      textView10.setText(c.getString(c.getColumnIndex("bmonth"))); 
      textView10.setClickable(true); 
      ett8=textView10.getText().toString(); 

      textView11 = new TextView(getApplicationContext()); 
      textView11.setText(c.getString(c.getColumnIndex("byear"))); 
      textView11.setClickable(true); 
      ett9=textView11.getText().toString(); 

      textView3.setPadding(10, 10, 10,10); 
      textView5.setPadding(10, 10, 10, 10); 
      tableRow.addView(textView3); 
      tableRow.addView(textView5); 
      tableLayout.addView(tableRow); 
      c.moveToNext(); 

      final List<String> list = new ArrayList<String>(); 
      list.add(textView3.getText().toString()); 
      list.add(textView4.getText().toString()); 
      list.add(textView5.getText().toString()); 
      list.add(textView6.getText().toString()); 
      list.add(textView7.getText().toString()); 
      list.add(textView8.getText().toString()); 
      list.add(textView9.getText().toString()); 
      list.add(textView10.getText().toString()); 
      list.add(textView11.getText().toString()); 

      textView3.setOnClickListener(new View.OnClickListener() { 



       @Override 
       public void onClick(View v) { 
        // TODO Auto-generated method stub 

        Intent intent = new Intent(FullExplanationEntry.this ,SavedData.class); 
        intent.putExtra("ett1",list.get(0) + ""); 
        intent.putExtra("ett2",list.get(1) + ""); 
        intent.putExtra("ett3",list.get(2) + ""); 
        intent.putExtra("ett4",list.get(3) + ""); 
        intent.putExtra("ett5",list.get(4) + ""); 
        intent.putExtra("ett6",list.get(5) + ""); 
        intent.putExtra("ett7",list.get(6) + ""); 
        intent.putExtra("ett8",list.get(7) + ""); 
        intent.putExtra("ett9",list.get(8) + ""); 

        startActivity(intent); 



       } 
      }); 
     } 


     c.close(); 
     setContentView(tableLayout); 
      db.close(); 
    } 
+2

隨意解釋一下「不工作」是什麼意思。 – laalto

+0

我做了創建名字,中間名和姓氏數據庫的代碼..它正在工作..但是當我添加日期... DOB ..應用程序崩潰...我給VARCHAR的名字......並給了NUMERIC對於日期...我認爲這個數字可能是錯誤的...但我嘗試給整數,文本,但它isalo崩潰 – roshanpeter

+2

崩潰,請包括異常stacktrace的問題。如果你添加了新的列,「CREATE TABLE IF NOT EXISTS」將不會執行任何操作,因爲表已經存在。 – laalto

回答

2

CREATE TABLE IF NOT EXISTS因爲它已經存在不會重新創建表。在創建表後添加的列不會出現,因爲列名無效,您將得到SQL語法錯誤。

根據評論,卸載解決了你的問題。這是因爲它刪除了舊的數據庫,然後用所有的列創建了一個新的數據庫。

對於未來的需求,可以考慮使用內置模式版本系統SQLiteOpenHelper的:onCreate()創建一個新的數據庫,並onUpgrade()從舊的數據庫模式版本向上遷移。

+0

謝謝拉爾託...我接受你的答案..... – roshanpeter

1

在SQLite的一個日期是字符串
以ISO格式('yyyy-MM-dd')傳遞它。
您還可以添加時間( 'YYYY-MM-DD HH:MM:SS')