2017-04-10 69 views
-1

我需要一點幫助。我正在嘗試將一些數據保存到sqllite,並且我繼續得到不成功的烤麪包。下面是我的代碼,我有相同的代碼爲第二個SQLite和它的作品?這裏似乎是什麼問題(也許是時間)。使用dbhelper在sqlite中保存數據

我的XML:

<EditText 
     android:id="@+id/moja_bolecina" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="Moja bolečina" /> 

    <EditText 
     android:id="@+id/moje_zdravilo" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="Zdravilo" /> 

    <EditText 
     android:id="@+id/kolicina_zdravila" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="Zdravilo vzamem (primer 3x dnevno)" /> 

    <TextView 
     android:id="@+id/editText3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textColor="#000" 
     android:textSize="20sp" 
     android:text="OB:" 
     android:paddingTop="5dp"/> 

    <EditText 
     android:id="@+id/ura_1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:ems="10" 
     android:inputType="time" /> 

    <EditText 
     android:id="@+id/ura_2" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:ems="10" 
     android:inputType="time" /> 

    <EditText 
     android:id="@+id/ura_3" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:ems="10" 
     android:inputType="time" 
     /> 

    <Button 
     android:id="@+id/shrani_nastavitve" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Shrani" /> 

    <Button 
     android:id="@+id/pregled_nastavitev" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Pregled Trenutnih nastavitev" /> 

我DBhelper:

public class DBhelper_nastavitve extends SQLiteOpenHelper { 

    public static final String DATABASE_NAME = "vas_podatki.db"; 
    public static final String TABLE_NAME = "nastavitve_uporabnika"; 
    public static final String COL_1 = "vrsta_bolecine"; 
    public static final String COL_2 = "zravilo_proti_bolecini"; 
    public static final String COL_3 = "zdravilo_vzamem"; 
    public static final String COL_4 = "prva_ura"; 
    public static final String COL_5 = "druga_ura"; 
    public static final String COL_6 = "tretja_ura"; 
    public static final String COLUMN_TIME_STAMP = "timeStamp "; 



    public DBhelper_nastavitve(Context context) { 
     super(context, DATABASE_NAME, null, 1); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     db.execSQL("create table "+ TABLE_NAME + "(COL_1 STRING, COL_2 STRING,COL_3 STRING, COL_4 STRING, COL_5 STRING, COL_6 STRING, COLUMN_TIME_STAMP TIMESTAMP DEFAULT CURRENT_TIMESTAMP)"); 

    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     db.execSQL("DROP TABLE IF EXISTS"+ TABLE_NAME); 
     onCreate(db); 

    } 

    public boolean insertData(String vrsta_bolecine, String zravilo_proti_bolecini, String zdravilo_vzamem, String prva_ura, String druga_ura, String tretja_ura){ 
     SQLiteDatabase db_nastavitve = this.getWritableDatabase(); 
     ContentValues contentValues = new ContentValues(); 
     contentValues.put(COL_1,vrsta_bolecine); 
     contentValues.put(COL_2,zravilo_proti_bolecini); 
     contentValues.put(COL_3,zdravilo_vzamem); 
     contentValues.put(COL_4,prva_ura); 
     contentValues.put(COL_5,druga_ura); 
     contentValues.put(COL_6,tretja_ura); 
     long reuslt = db_nastavitve.insert(TABLE_NAME,null,contentValues); 
     if (reuslt == -1) 
      return false; 
     else 
      return true; 
    } 
} 

主類:

public class nastavitve extends AppCompatActivity { 

    Button gumb_poslji; 
    DBhelper_nastavitve myDb_nastavitve; 
    EditText bolecina, zdravilo, kolicina_zdravila, ura1, ura2, ura3; 

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

     gumb_poslji=(Button)findViewById(R.id.shrani_nastavitve); 
     myDb_nastavitve = new DBhelper_nastavitve(this); 
     bolecina = (EditText)findViewById(R.id.moja_bolecina); 
     zdravilo = (EditText)findViewById(R.id.moje_zdravilo); 
     kolicina_zdravila = (EditText)findViewById(R.id.kolicina_zdravila); 
     ura1 = (EditText)findViewById(R.id.ura_1); 
     ura2 = (EditText)findViewById(R.id.ura_2); 
     ura3 = (EditText)findViewById(R.id.ura_3); 

     AddData(); 

    } 
    public void AddData(){ 

     gumb_poslji.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       boolean pregled_vnosa = myDb_nastavitve.insertData(
         bolecina.getText().toString(), 
         zdravilo.getText().toString(), 
         kolicina_zdravila.getText().toString(), 
         ura1.getText().toString(), 
         ura2.getText().toString(), 
         ura3.getText().toString() 
       ); 
       if (pregled_vnosa==true) 
        Toast.makeText(nastavitve.this, "Vnos uspešen",Toast.LENGTH_LONG).show(); 
       else 
        Toast.makeText(nastavitve.this, "Vnos neuspešen!!!",Toast.LENGTH_LONG).show(); 
      } 
     }); 

    } 
} 
+0

把你的堆棧跟蹤放在這裏 –

回答

0

,所以我改變了這一行:

db.execSQL("create table "+ TABLE_NAME + "(COL_1 STRING, COL_2 STRING,COL_3 STRING, COL_4 STRING, COL_5 STRING, COL_6 STRING, COLUMN_TIME_STAMP TIMESTAMP DEFAULT CURRENT_TIMESTAMP)"); 

對此:

db.execSQL("create table "+ TABLE_NAME + "(vrsta_bolecine STRING, zravilo_proti_bolecini STRING,zdravilo_vzamem STRING, prva_ura STRING, druga_ura STRING, tretja_ura STRING, COLUMN_TIME_STAMP TIMESTAMP DEFAULT CURRENT_TIMESTAMP)"); 

它的工作原理。