Reputation: 31
sqlite sql syntax error near '('
<code>
@Override
public void onCreate(SQLiteDatabase arg0) {
StringBuilder sb = new StringBuilder();
sb.append("create table IF NOT EXISTS " + tableName + "(" );
sb.append(" eng TEXT primary key,");
sb.append(" gra TEXT not null,");
sb.append(" memo TEXT null,");
sb.append(" fsen TEXT null,");
sb.append(" ox INTEGER not null DEFAULT 0,");
sb.append(" oxSeq TEXT null,");
sb.append(" timeCreate TEXT not null DEFAULT datetime('now', 'localtime'),");
sb.append(" timeLast TEXT not null DEFAULT datetime('now', 'localtime') )");
arg0.execSQL(sb.toString());
Log.w(" -- Result -- ", tableName + " is created.");
}
////Error message is like following...
android.database.sqlite.SQLiteException: near "(":
syntax error (code 1): , while compiling:
create table IF NOT EXISTS userDic(
eng TEXT primary key,
kor TEXT not null,
memo TEXT null,
fsen TEXT null,
ox INTEGER not null DEFAULT 0,
oxSeq TEXT null,
timeCreate TEXT not null DEFAULT datetime('now', 'localtime'),
timeLast TEXT not null DEFAULT datetime('now', 'localtime') )
</code>
I don't know why the error is occurred... I don't know why the error is occurred... I don't know why the error is occurred... I don't know why the error is occurred... I don't know why the error is occurred... I don't know why the error is occurred...
Upvotes: 0
Views: 1316
Reputation: 31
Actually, I found the answer with others' idea.
Problem is DEFAULT
I need to change the part
DEFAULT datetime('now', 'localtime')
to
DEFAULT(datetime('now', 'localtime'))
()
is needed. Now it's working.
Upvotes: 0
Reputation: 152817
To use a default value, replace datetime('now', 'localtime')
with e.g. CURRENT_TIMESTAMP
. Note that it won't be localtime but UTC zulu time.
To use an expression as a default, it needs to be in ()
parentheses, e.g. DEFAULT(datetime('now', 'localtime'))
.
https://www.sqlite.org/lang_createtable.html
Upvotes: 2
Reputation: 76466
tableName
needs to be surrounded in single quotes:
corrected:
sb.append("create table IF NOT EXISTS '" + tableName + "' (" );
Upvotes: -1