2013-12-07 4 views
2

Я новый в SQLite. Я пытаюсь использовать SQLite в Android. Но я сталкиваюсь с такой ошибкой, когда я запускаю ее.Ошибка 1: Ошибка синтаксиса

Ошибка:

Failure 1 (near "tableuserInfoTable": syntax error) on 0x14d2c8 when preparing 'create tableuserInfoTable(_id integer primary key name text not nullemailtext not nullpasswordtext not nulltimetext not null);' .

Код:

import android.content.Context; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.util.Log; 

public class DBHelper extends SQLiteOpenHelper { 
    public static final String DATABASE_NAME="data"; 
    public static final int DATABASE_VERSION=1; 

    public static final String USER_TABLE="userTable"; 
    public static final String C_ID="_id"; 
    public static final String USER="name"; 
    public static final String EMAIL="email"; 
    public static final String PASSWORD="password"; 
    public static final String TIME="time"; 

    public final String createDB="create table"+USER_TABLE+"("+C_ID+" integer primary key " 
          +USER+" text not null" +EMAIL+ "text not null" +PASSWORD+ "text not null" 
          +TIME+ "text not null);"; 

    public DBHelper(Context context) 
    { 
     super(context,DATABASE_NAME, null, DATABASE_VERSION); 
     // TODO Auto-generated constructor stub 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     // TODO Auto-generated method stub 
     db.execSQL(createDB); 
    } 


@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    // TODO Auto-generated method stub 
    if (newVersion > oldVersion) { 
     Log.w("MyAppTag","Updating database from version " + oldVersion + " to " 
       + newVersion + " .Existing data will be lost."); 
    db.execSQL("drop table if exists"+USER_TABLE); 
    onCreate(db); 
} 
} 
} 

Подхожу почти каждый один пример из SO, но я не мог понять. Спасибо

ответ

3

Попробуйте

public final String createDB="create table "+USER_TABLE+"(" 
         +C_ID+" integer primary key, " 
         +USER+" text not null," 
         +EMAIL+ " text not null," 
         +PASSWORD+ " text not null," 
         +TIME+ " text not null);"; 

Пару ошибок: - У вас не хватает пространства между столом и TABLENAME; - Вы пропускаете запятые в конце колонны, - Вы также отсутствуют пробелы между именами столбцов и типов данных:

Вы всегда должны войти запрос и попытаться запустить его в базу данных напрямую. Ошибки станут очевидными очень быстро.

+0

+1 За уведомление оповещения пробелов. – gahfy

+0

Теперь он работает. Спасибо вам всем. –

3

В вашем запросе нет запятой, и вы пропустили пробелы.

Try:

public final String createDB="create table "+USER_TABLE+"("+C_ID+" integer primary key, " 
         +USER+" text not null," +EMAIL+ " text not null, " +PASSWORD+ " text not null," 
         +TIME+ " text not null);"; 
+1

Там также нет пространства между «создать таблицу» и имя таблицы. –

+0

Спасибо, что отправили его в ноту. Исправлена. – gahfy

0

Ошибка 1 (около «tableuserInfoTable»: синтаксическая ошибка) на 0x14d2c8 при подготовке create tableuserInfoTable(_id integer primary key name text not nullemailtext not nullpasswordtext not nulltimetext not null);.

  • Этот тип ошибки отсутствует. Имя табуляции (если оно неверно) или запятая или пробел или тип данных отсутствует.

Решение Если имя таблицы tableuserInfoTable затем начинает запрос:

create table tableuserInfoTable(
_id integer primary key name text not null, 
email text not null, 
password text not null, 
time text not null); 
Смежные вопросы