DATABASE HANDLER JAVA FILE
public class MyDbHandler extends SQLiteOpenHelper{
public static final int DATABASE_VERSION =1;
public static final String DATABASE_NAME ="Dataa";
public static final String TABLE_NAME="Dataa";
public static final String CLOUMN_ID="_id";
public static final String CLOUMN_FIRST_NAME="_FirstName";
public static final String CLOUMN_LAST_NAME="_LastName";
public static final String CLOUMN_GENDER="_Gender";
public static final String CLOUMN_CITY="_City";
public MyDbHandler(Context context, String name , SQLiteDatabase.CursorFactory factory, int version) {
super(context , DATABASE_NAME, factory,DATABASE_VERSION );
}
@Override
public void onCreate(SQLiteDatabase db) {
String query = "CREATE TABLE " + TABLE_NAME + "("+
CLOUMN_ID +" INTEGER PRIMARY KEY AUTOINCREMENT " +
CLOUMN_FIRST_NAME +" TEXT " +
CLOUMN_LAST_NAME +" TEXT " +
CLOUMN_GENDER +" TEXT " +
CLOUMN_CITY +" TEXT " +
");";
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXIST " + TABLE_NAME );
onCreate(db);
}
//Adding Record
public void addData(Dataa dataaa )
{
ContentValues values= new ContentValues();
values.put(CLOUMN_FIRST_NAME, dataaa.get_FirstName());
values.put(CLOUMN_LAST_NAME, dataaa.get_LastName());
values.put(CLOUMN_GENDER, dataaa.get_Gender());
values.put(CLOUMN_CITY, dataaa.getCity());
SQLiteDatabase db = getWritableDatabase();
db.insert(TABLE_NAME, null, values);
db.close();
}
//Print
public String databaseToString()
{
String dbString="";
SQLiteDatabase db= getWritableDatabase();
String query= "SELECT * FROM " + TABLE_NAME +" WHERE 1";
//cURSOR
Cursor c= db.rawQuery(query,null);
//move to first row
c.moveToFirst();
while(!c.isAfterLast())
{
if(c.getString(c.getColumnIndex("_FirstName"))!= null)
{
dbString +=c.getString(c.getColumnIndex("_FirstName"));
dbString +="\n";
}
}
db.close();
return dbString;
}
}
ERROR SCREENSHOT
Caused by: android.database.sqlite.SQLiteException: near "_FirstName": syntax error (code 1): , while compiling: CREATE TABLE Dataa(_idINTEGER PRIMARY KEY AUTOINCREMENT _FirstName TEXT _LastName TEXT _Gender TEXT _City TEXT );
Error Code : 1 (SQLITE_ERROR)
Caused By : SQL(query) error or missing database.
(near "_FirstName": syntax error (code 1): , while compiling: CREATE TABLE Dataa(_idINTEGER PRIMARY KEY AUTOINCREMENT _FirstName TEXT _LastName TEXT _Gender TEXT _City TEXT );)
#################################################################
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1058)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:623)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:59)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1812)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1743)
at com.example.useraccountname.sqli1.MyDbHandler.onCreate(MyDbHandler.java:38)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
at com.example.useraccountname.sqli1.MyDbHandler.addData(MyDbHandler.java:56)
at com.example.useraccountname.sqli1.Ma
答案 0 :(得分:3)
只需在TEXT
或数据类型之后添加逗号(,),如下所示
String query = "CREATE TABLE " + TABLE_NAME + "("+
CLOUMN_ID +" INTEGER PRIMARY KEY AUTOINCREMENT, " +
CLOUMN_FIRST_NAME +" TEXT, " +
CLOUMN_LAST_NAME +" TEXT, " +
CLOUMN_GENDER +" TEXT, " +
CLOUMN_CITY +" TEXT " +
");";
db.execSQL(query);
答案 1 :(得分:2)
以这种方式纠正您的查询
String query =
"CREATE TABLE " + TABLE_NAME + " ("
+ CLOUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT ," + CLOUMN_FIRST_NAME + " TEXT ,"
+ CLOUMN_LAST_NAME + " TEXT ,"
+ CLOUMN_GENDER + " TEXT ,"
+ CLOUMN_CITY + " TEXT" + ")";
db.execSQL(query);
然后卸载旧应用并重新运行。