私は Android の初心者で、2 つのテーブルを持つデータベースを作成しようとしていますが、プロジェクトを起動すると、logcat で次のエラーが発生しました。
05-04 11:21:29.678 1634-1634/com.example.keco.myapplication E/SQLiteLog﹕
(1) near "TABLEstudents": syntax error
05-04 11:21:29.680 1634-1634/com.example.keco.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.keco.myapplication, PID: 1634
java.lang.RuntimeException: Unable to resume activity {com.example.keco.myapplication/com.example.keco.myapplication.MainActivity}: android.database.sqlite.SQLiteException: near "TABLEstudents": syntax error (code 1): , while compiling: CREATE TABLEstudents(_id INTEGER PRIMARY KEY AUTOINCREMENT, _name VARCHAR(30))
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2951)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2982)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2365)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: android.database.sqlite.SQLiteException: near "TABLEstudents": syntax error (code 1): , while compiling: CREATE TABLEstudents(_id INTEGER PRIMARY KEY AUTOINCREMENT, _name VARCHAR(30))
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1674)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1605)
at com.example.keco.myapplication.SqlGenerator.onCreate(SqlGenerator.java:38)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:187)
at com.example.keco.myapplication.DatabaseSource.<init>(DatabaseSource.java:17)
at com.example.keco.myapplication.MainActivity.onResume(MainActivity.java:63)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1241)
at android.app.Activity.performResume(Activity.java:6019)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2940)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2982)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2365)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
SQLiteOpenHelper を拡張するクラスは次のとおりです。
package com.example.keco.myapplication;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
/**
* Created by Keco on 04/05/2015.
*/
public class SqlGenerator extends SQLiteOpenHelper {
private static final String STUDENTS_TABLE="students";
private static final String COLUMN_name="_name";
private static final String DB_NAME="school.db";
private static final int DB_VERSION=1;
public static final String CREATE_STUDENTS_TABLE=
" CREATE TABLE"+ STUDENTS_TABLE+"("+ BaseColumns._ID+" INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_name+" VARCHAR(30)) ";
public SqlGenerator (Context context){
super(context,DB_NAME,null,DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(CREATE_STUDENTS_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
コードをきれいにするスタイルとして、次のように SqlGenerator オブジェクトを別のクラスで宣言します。
package com.example.keco.myapplication;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
/**
* Created by Keco on 04/05/2015.
*/
public class DatabaseSource {
private Context mContext;
private SqlGenerator mSqlGenerator;
public DatabaseSource (Context context){
mContext=context;
mSqlGenerator=new SqlGenerator(context);
SQLiteDatabase database=mSqlGenerator.getReadableDatabase();
database.close();
}
}
MainActivity では、次のように DatabaseSource オブジェクトを宣言して初期化します。
package com.example.keco.myapplication;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onResume() {
super.onResume();
DatabaseSource databaseSource=new DatabaseSource(this);
}
}
あなたの感謝の助けを待っています...