下面顯示的代碼嘗試創建表格並定義用於向表格中輸入數據的功能。android中的sqlite無法創建表格
public class Showtime extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "cinema";
// Crew table name
private static final String TABLE_CREW = "crew";
// Crew Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_CAST = "cast";
private static final String KEY_PRODUCER = "producer";
private static final String KEY_WRITER = "writer";
private static final String KEY_DIRECTOR = "director";
public Showtime(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CREW_TABLE = "CREATE TABLE" + TABLE_CREW + "("
+ KEY_ID + "INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_CAST + "TEXT,"
+ KEY_WRITER + "TEXT," + KEY_DIRECTOR + "TEXT," + KEY_PRODUCER + "TEXT" +")";
db.execSQL(CREATE_CREW_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CREW);
// Create tables again
onCreate(db);
}
public void addCrew(Crew crew) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_CAST, crew.getCast()); // actors or cast
values.put(KEY_WRITER, crew.getWriters()); // writers name list
values.put(KEY_DIRECTOR, crew.getDirectors()); // directors name list
values.put(KEY_PRODUCER, crew.getProducers()); // producers name list
// Inserting Row
db.insert(TABLE_CREW, null, values);
db.close(); // Closing database connection
}
.......
我嘗試將數據插入到上面使用下面的代碼顯示創建表:
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.showtime);
Showtime db = new Showtime(this);
/**
* CRUD Operations
* */
// Inserting Crews
Log.d("Insert: ", "Inserting ..");
//Crew (String _cast , String _writers ,String _directors , String _producers)
db.addCrew(new Crew("tom hanks","Billy Ray","Paul Greengrass" ,"Michael De Luca"));
但我收到此錯誤:
02-12 13:24:49.855: D/Insert:(1450): Inserting ..
02-12 13:24:49.903: E/SQLiteLog(1450): (1) table crew has no column named producer
02-12 13:24:49.907: E/SQLiteDatabase(1450): Error inserting producer=Michael De Luca director=Billy Ray writer=Billy Ray cast=tom hanks
02-12 13:24:49.907: E/SQLiteDatabase(1450): android.database.sqlite.SQLiteException: table crew has no column named producer (code 1): , while compiling: INSERT INTO crew(producer,director,writer,cast) VALUES (?,?,?,?)
02-12 13:24:49.907: E/SQLiteDatabase(1450): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
02-12 13:24:49.907: E/SQLiteDatabase(1450): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
02-12 13:24:49.907: E/SQLiteDatabase(1450): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
02-12 13:24:49.907: E/SQLiteDatabase(1450): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
02-12 13:24:49.907: E/SQLiteDatabase(1450): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
02-12 13:24:49.907: E/SQLiteDatabase(1450): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
02-12 13:24:49.907: E/SQLiteDatabase(1450): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
02-12 13:24:49.907: E/SQLiteDatabase(1450): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
02-12 13:24:49.907: E/SQLiteDatabase(1450): at com.example.test.Showtime.addCrew(Showtime.java:61)
02-12 13:24:49.907: E/SQLiteDatabase(1450): at com.example.test.MainActivity.onCreate(MainActivity.java:24)
02-12 13:24:49.907: E/SQLiteDatabase(1450): at android.app.Activity.performCreate(Activity.java:5133)
02-12 13:24:49.907: E/SQLiteDatabase(1450): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-12 13:24:49.907: E/SQLiteDatabase(1450): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
02-12 13:24:49.907: E/SQLiteDatabase(1450): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
02-12 13:24:49.907: E/SQLiteDatabase(1450): at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-12 13:24:49.907: E/SQLiteDatabase(1450): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
02-12 13:24:49.907: E/SQLiteDatabase(1450): at android.os.Handler.dispatchMessage(Handler.java:99)
02-12 13:24:49.907: E/SQLiteDatabase(1450): at android.os.Looper.loop(Looper.java:137)
02-12 13:24:49.907: E/SQLiteDatabase(1450): at android.app.ActivityThread.main(ActivityThread.java:5103)
02-12 13:24:49.907: E/SQLiteDatabase(1450): at java.lang.reflect.Method.invokeNative(Native Method)
02-12 13:24:49.907: E/SQLiteDatabase(1450): at java.lang.reflect.Method.invoke(Method.java:525)
02-12 13:24:49.907: E/SQLiteDatabase(1450): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
02-12 13:24:49.907: E/SQLiteDatabase(1450): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-12 13:24:49.907: E/SQLiteDatabase(1450): at dalvik.system.NativeStart.main(Native Method)
02-12 13:24:49.915: D/Reading:(1450): Reading all contacts..
02-12 13:24:50.127: D/libEGL(1450): loaded /system/lib/egl/libEGL_genymotion.so
02-12 13:24:50.167: D/(1450): HostConnection::get() New Host Connection established 0xb7dddf98, tid 1450
02-12 13:24:50.215: D/libEGL(1450): loaded /system/lib/egl/libGLESv1_CM_genymotion.so
02-12 13:24:50.219: D/libEGL(1450): loaded /system/lib/egl/libGLESv2_genymotion.so
02-12 13:24:50.539: W/EGL_genymotion(1450): eglSurfaceAttrib not implemented
02-12 13:24:50.543: E/OpenGLRenderer(1450): Getting MAX_TEXTURE_SIZE from GradienCache
02-12 13:24:50.567: E/OpenGLRenderer(1450): Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
02-12 13:24:50.567: D/OpenGLRenderer(1450): Enabling debug mode 0
02-12 13:24:50.607: D/dalvikvm(1450): GC_FOR_ALLOC freed 161K, 24% free 2811K/3688K, paused 14ms, total 17ms
Thank you for your help!
Write以正確的方式創建查詢:'在列類型和名稱之間添加空格' –
格式代碼涉及長字符串:http://stackoverflow.com/a/17390617/1531054 –