0

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...

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

I see... so... I need to change the part "DEFAULT datetime('now', 'localtime')" as CURRENT_TIMESTAMP...
0

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.

Comments

-1

tableName needs to be surrounded in single quotes:

corrected:

 sb.append("create table IF NOT EXISTS '" + tableName + "' (" );

2 Comments

@FrankN.Stein oh am I wrong? Just went and looked it up in my project and am using single quotes around the table name, then again i am not using .execSQL
Try removing them, it will work as well (if the table name is a single word). OR maybe your table name contains spaces (but I use [ and ] in that case).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.