我正在尝试在Android中创建一个新的SQLite数据库。 这是我的onCreate方法:
public static final String DATABASE_NAME = "eventList.db";
public static final String TABLE_NAME = "event_table";
public static final String COL1 = "ID";
public static final String COL2 = "EVENTNAME";
public static final String COL3 = "UNIXTIMESTAMP";
public static final String COL4 = "PARTICIPANTS";
public static final String COL5 = "LOCATION";
public static final String COL6 = "LOCATIONNAME";
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
String createTable = "CREATE TABLE " + TABLE_NAME + "(ID INTEGER PRIMARY KEY AUTOINCRIMENT, "
+ " EVENTNAME TEXT, UNIXTIMESTAMP INTEGER, PARTICIPANTS TEXT, LOCATION TEXT, LOCATIONNAME TEXT)";
sqLiteDatabase.execSQL(createTable);
}
这会引发语法错误:
android.database.sqlite.SQLiteException:near“AUTOINCRIMENT”:语法错误(代码1):,编译时:CREATE TABLE event_table(ID INTEGER PRIMARY KEY AUTOINCRIMENT,EVENTNAME TEXT,UNIXTIMESTAMP INTEGER,PARTICIPANTS TEXT,LOCATION TEXT,LOCATIONNAME TEXT)
请注意PARTICIPANTS将成为自定义对象,LOCATION将成为LatLng对象。
如何解决错误?
答案 0 :(得分:2)
<!DOCTYPE html>
<head>
<link rel='stylesheet' type='text/css' href = 'routes.css'>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body></body>
<script>
//o is an open space x is a wall
var a = [['0','o','x','x'],
['x','o','o','o'],
['x','o','x','o'],
['x','o','o','g']];
/*printed grid looks like this
var a = [[0,1,'x','x'],
['x',NaN,NaN,NaN],
['x',NaN,'x',NaN],
['x',NaN,NaN,'g']];*/
function move(startR,startC){ //(0,0) startR,startC
var north = startR-1;
var south = startR+1;
var east = startC+1;
var west = startC-1;
//discard all the out of bounds locations
if (north<0){north=startR;}
if (south>3){south=startR;}
if (east>3){east=startC;}
if (west<0){west=startC;}
if (a[north][startC]=='o'){newLocation(north,startC,startR,startC);}
if (a[south][startC]=='o'){newLocation(south,startC,startR,startC);}
if (a[startR][west]=='o'){newLocation(startR,west,startR,startC);}
if (a[startR][east]=='o'){newLocation(startR,east,startR,startC);}
}
//newLocation() increments square I just moved to and pass the new location to the function 'move '
function newLocation(northSouth,eastWest,origR,origC){
var origValue = parseInt(a[origR,origC]);
console.log(origValue); //prints NaN to console
var newValue = ++origValue;
a[northSouth][eastWest]=newValue;
console.log(a[origR,origC]);
return move(northSouth,eastWest);
}
move(0,0);
//print board to screen
for (r=0;r<4;r++){
document.write('<br>');
for (c=0;c<4;c++){
var l = document.createElement('div');
var t = document.createTextNode(a[r][c]);
$(l).addClass('letterboxes');
l.appendChild(t);
document.body.appendChild(l);
// document.body.appendChild(letter);
}
}
</script>
</html>
不正确应该是ID INTEGER PRIMARY KEY AUTOINCRIMENT
然而,根据SQLite Autoincrement ,编码AUTOINCREMENT可能最好不编码。 ID仍将是唯一标识符。
您可能还会发现使用 _id 代替 ID 作为列名称是有利的。有时需要 _id ,例如至于ID INTEGER PRIMARY KEY AUTOINCREMENT
。
所以我建议使用CursorAdapters
答案 1 :(得分:0)
你输错了
String createTable = "CREATE TABLE " + TABLE_NAME + "(ID INTEGER PRIMARY KEY AUTOINCRIMENT, "
请注意,AUTOINCRIMENT
拼写为AUTOINCREMENT
加ID应为_id 此外,由于你已经设置常量的麻烦,为什么不使用它们来节省进一步的潜在错别字?例如 而不是
+ " EVENTNAME TEXT
为什么不使用
+ COL2 + " TEXT
等...
我可否建议您阅读有关如何创建数据库here的信息 这是一个极好的教程,并显示了最佳实践
答案 2 :(得分:0)
试试这个
public static final String DATABASE_NAME = "eventList.db";
public static final String TABLE_NAME = "event_table";
public static final String COL1 = "ID";
public static final String COL2 = "EVENTNAME";
public static final String COL3 = "UNIXTIMESTAMP";
public static final String COL4 = "PARTICIPANTS";
public static final String COL5 = "LOCATION";
public static final String COL6 = "LOCATIONNAME";
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
String createTable = "CREATE TABLE " + TABLE_NAME + "(ID INTEGER PRIMARY KEY AUTOINCREMENT, "
+ " EVENTNAME TEXT, UNIXTIMESTAMP INTEGER, PARTICIPANTS TEXT, LOCATION TEXT, LOCATIONNAME TEXT)";
sqLiteDatabase.execSQL(createTable);
}
我认为你拼写错误的错误 将AUTOINCRIMENT替换为AUTOINCREMENT。