我们在第一次插入时插入表中的数据(如第二次输入)它没有,如果你不能阅读和理解我的代码那么可能的原因那么我们可以讨论原因< / p>
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 :(得分:1)
可能这是问题吗?
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” 指定了子句。