我知道这个问题有一些答案,但我似乎仍然无法让我的工作。我收到错误消息;
04-17 11:35:35.086 2437-2437/com.chris.cv10aajproject E/SQLiteLog﹕ (1)
AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY
04-17 11:35:35.090 2437-2437/com.chris.cv10aajproject E/SQLiteLog﹕ (1)
no such table: viewing
我的代码如下:
我的适配器中有InsertData
方法,由我的应用程序调用
public long insertData(String posCode,String firstLine ,String town,String county, int roomNum,
int askingPrice, int currentOffer, int agreedPrice, String agentName,
String agentPhone,String estateAgentNam, int refurb, String Time)
{
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(DbHelper.POST_CODE, posCode);
contentValues.put(DbHelper.FIRST_LINE,firstLine);
contentValues.put(DbHelper.TOWN,town);
contentValues.put(DbHelper.COUNTY, county);
contentValues.put(DbHelper.ROOM_NO,roomNum);
contentValues.put(DbHelper.ASKING_PRICE,askingPrice);
contentValues.put(DbHelper.CURRENT_OFFER, currentOffer);
contentValues.put(DbHelper.AGREED_PRICE,agreedPrice);
contentValues.put(DbHelper.AGENT_NAME,agentName);
contentValues.put(DbHelper.AGENT_PHO_NUM, agentPhone);
contentValues.put(DbHelper.ESTATE_AGENT_NAME,estateAgentNam);
contentValues.put(DbHelper.REFURB_COST,refurb);
contentValues.put(DbHelper.TIME_OF_APPOINTMENT,Time);
long id = db.insert(DbHelper.TABLE_NAME, null, contentValues);
return id ;
我的on create方法看起来像这样
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(CREATE_TABLE);
Message.message(context, "onCreate called");
} catch (SQLException e) {
Message.message(context, ""+e);
}
}
并且CREATE_TABLE
是一个常量,如下所示:
private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (" + UID + "INTEGER PRIMARY KEY AUTOINCREMENT, " + POST_CODE + " VARCHAR(255)," +FIRST_LINE+" VARCHAR(255)," + TOWN + " VARCHAR(255) " +
"" + COUNTY + " VARCHAR(255) ," + ROOM_NO + " INTEGER ," + ASKING_PRICE + " INTEGER , " + CURRENT_OFFER + " INTEGER , " + AGREED_PRICE + "INTEGER , " + AGENT_NAME + " VARCHAR(255)," +
"" + AGENT_PHO_NUM + " VARCHAR(255), " + ESTATE_AGENT_NAME + " VARCHAR(255), " + REFURB_COST + " VARCHAR(255), " + TIME_OF_APPOINTMENT + " VARCHAR(255));";
欢迎任何建议
答案 0 :(得分:4)
您需要在UID
和INTEGER
所以改变
" (" + UID + "INTEGER PRIMARY KEY AUTOINCREMENT,
到
" (" + UID + " INTEGER PRIMARY KEY AUTOINCREMENT,
和
"INTEGER , " + AGENT_NAME +
到
" INTEGER , " + AGENT_NAME +
因此将create table语句重写为
private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (" + UID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + POST_CODE + " VARCHAR(255)," +FIRST_LINE+" VARCHAR(255)," + TOWN + " VARCHAR(255) " +
"" + COUNTY + " VARCHAR(255) ," + ROOM_NO + " INTEGER ," + ASKING_PRICE + " INTEGER , " + CURRENT_OFFER + " INTEGER , " + AGREED_PRICE + " INTEGER , " + AGENT_NAME + " VARCHAR(255)," +
"" + AGENT_PHO_NUM + " VARCHAR(255), " + ESTATE_AGENT_NAME + " VARCHAR(255), " + REFURB_COST + " VARCHAR(255), " + TIME_OF_APPOINTMENT + " VARCHAR(255));";
答案 1 :(得分:2)
您的表未创建,因为您的CREATE TABLE SQL
命令错误。
您应该在Column Name
和Column Type
TABLE_NAME + " (" + UID + " INTEGER PRIMARY KEY AUTOINCREMENT, // add space before INTEGER PRIMARY KEY AUTOINCREMENT
private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (" + UID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + POST_CODE + " VARCHAR(255)," +FIRST_LINE+" VARCHAR(255)," + TOWN + " VARCHAR(255) " +
"" + COUNTY + " VARCHAR(255) ," + ROOM_NO + " INTEGER ," + ASKING_PRICE + " INTEGER , " + CURRENT_OFFER + " INTEGER , " + AGREED_PRICE + " INTEGER , " + AGENT_NAME + " VARCHAR(255)," +
"" + AGENT_PHO_NUM + " VARCHAR(255), " + ESTATE_AGENT_NAME + " VARCHAR(255), " + REFURB_COST + " VARCHAR(255), " + TIME_OF_APPOINTMENT + " VARCHAR(255));";
答案 2 :(得分:0)
您需要添加空格并且在TOWN中缺少逗号,请执行以下操作:
private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (" + UID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + POST_CODE + " VARCHAR(255)," + FIRST_LINE + " VARCHAR(255)," + TOWN + " VARCHAR(255), " + COUNTY + " VARCHAR(255) ," + ROOM_NO + " INTEGER ," + ASKING_PRICE + " INTEGER , " + CURRENT_OFFER + " INTEGER , " + AGREED_PRICE + "INTEGER , " + AGENT_NAME + " VARCHAR(255)," + AGENT_PHO_NUM + " VARCHAR(255), " + ESTATE_AGENT_NAME + " VARCHAR(255), " + REFURB_COST + " VARCHAR(255), " + TIME_OF_APPOINTMENT + " VARCHAR(255));";