2016-01-02 124 views
3

我們在表格第一次插入而第二次插入數據的時候插入的數據(如第二個條目)它不是,應該可以做什麼如果你不能閱讀和理解我的代碼的原因,那麼我們可以討論的原因SQlite:數據只在表格中第一次插入,第二次不插入

package com.example.sarahn.locationactivity; 

import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.SQLException; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.widget.Toast; 


public class HelperAdaptor extends SQLiteOpenHelper{ 
public static final String DATABASE_NAME = "mmhdatabase"; 
public static final String TABLE_NAME3 = "timetable"; 
public static final int DATABASE_VERSION = 2; 
public static final String NAME1 = "_timeprofilename"; 
public static final String TIME_PROFILE = "selecteddtimeprofile"; 
public static final String START_HOUR = "starthour"; 
public static final String END_HOUR = "endhour"; 
public static final String START_MINUTE = "startmin"; 
public static final String END_MINUTE = "endmin"; 
public static final String CREATE_TABLE3 = " CREATE TABLE " +TABLE_NAME3+ " ("+NAME1+ " STRING PRIMARY KEY, " +START_HOUR+ " INTEGER, " +END_HOUR+ "  INTEGER," +START_MINUTE+ " INTEGER, " +END_MINUTE+ " INTEGER, "   +TIME_PROFILE+ " INTEGER); " ; 
public static final String DROP_TABLE1 = " DROP TABLE IF EXISTS "   +TABLE_NAME3; 


public Context context; 

public HelperAdaptor (Context context) { 
    super(context,DATABASE_NAME, null, DATABASE_VERSION); 
    this.context= context; 

} 


@Override 
public void onCreate(SQLiteDatabase db) { 


    try { 

     db.execSQL(CREATE_TABLE3); 

    }catch (SQLException e) 
    { 

    } 

} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 

    try { 

     db.execSQL(DROP_TABLE1); 


     onCreate(db); 


    }catch (SQLException e) 
    { 

    } 

} 


public boolean insertdatatime(String nametime, int hrstart , int hrend ,  int minstart, int minend, int pr) { 

    SQLiteDatabase db = this.getWritableDatabase(); 
    ContentValues contentValues1 = new ContentValues(); 
    contentValues1.put(this.NAME1, nametime); 
    contentValues1.put(this.START_HOUR, hrstart); 
    contentValues1.put(this.END_HOUR, hrend); 
    contentValues1.put(this.START_MINUTE, minstart); 
    contentValues1.put(this.END_MINUTE, minend); 
    contentValues1.put(this.TIME_PROFILE, pr); 

    long id = db.insert(this.TABLE_NAME3, null, contentValues1); 
    if(id == -1) 
    { 
     return false; 
    } 
    else { 
     return true; 
    } 

} 


} 
+0

第一次創建,第二次更新。 –

回答

0

可能是這個問題?

public static final String CREATE_TABLE3 = " CREATE TABLE " +TABLE_NAME3+ " ("+NAME1+ " STRING PRIMARY KEY, " +START_HOUR+ " INTEGER, " +END_HOUR+ "  INTEGER," +START_MINUTE+ " INTEGER, " +END_MINUTE+ " INTEGER, "   +TIME_PROFILE+ " INTEGER); " ; 

public static final String CREATE_TABLE3 = " CREATE TABLE IF NOT EXISTS " +TABLE_NAME3+ " ("+NAME1+ " STRING PRIMARY KEY, " +START_HOUR+ " INTEGER, " +END_HOUR+ "  INTEGER," +START_MINUTE+ " INTEGER, " +END_MINUTE+ " INTEGER, "   +TIME_PROFILE+ " INTEGER); " ; 

它通常是試圖在數據庫 已經包含表,索引或視圖同名創建一個新表的錯誤。 但是,如果將「IF NOT EXISTS」子句指定爲 CREATE TABLE語句的一部分,並且存在已存在 的同名表或視圖,則CREATE TABLE命令根本無效(並且沒有錯誤 消息被返回)。即使指定了「IF NOT EXISTS」 子句,但由於存在索引而無法創建表 ,仍會返回錯誤。

相關問題