android中的sqlite无法创建表

时间:2014-02-12 10:39:04

标签: java 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
    }
                      .......

我尝试使用下面的代码将数据插入上面的create table:

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!

2 个答案:

答案 0 :(得分:2)

列名和类型之间以及SQL关键字和标识符(TABLE,表名)之间需要空格。

更改

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" +")";

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" +")";

编辑表架构后,卸载您的应用,以便删除旧的数据库文件并再次运行onCreate()

答案 1 :(得分:2)

试试这个:

  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" +")";

你写错了查询方式。您需要在列类型和名称之间添加空格。