创建数据库表

时间:2013-03-05 11:14:16

标签: android sql database

我是Android新手,我想为我的应用程序创建一个包含10列的数据库表。我试图找一些关于数据库的参考来创建一个表。但它没有用。请告诉我这个问题。 这是我的getter和setter方法:

    public class Name {

    String name;
    String described;
    String da1;
    String da2;
    String da3;
    String da4;
    String da5;
    String da6;
    String da7;
    String da8;

    public Name()
    {

    }

    public Name(String name, String described, String da1,String da2,String da3,String  da4,String da5,String da6,String da7,String da8)
    {
        this.described = described;
        this.name = name;
        this.da1 = da1;
        this.da2 = da2;
        this.da3 = da3;
        this.da4 = da4;
        this.da5 = da5;
        this.da6 = da6;
        this.da7 = da7;
        this.da8 = da8;
    }
//-------------------------------------------   
    public String getName()
    {
    return this.name;
    }
    public void SetName(String name)
    {
    this.name = name;
    }
//--------------------------------------------  
    public String getDescribed()
   {
    return this.described;
    }
    public void SetDescribed(String described)
    {
    this.described = described;
    }
//---------------------------------------
public String getDa1()
{
    return this.da1;
}
public void SetDa1(String da1)
{
    this.da1 = da1;
}
//--------------------------------------------
public String getDa2()
{
    return this.da2;
}
public void SetDa2(String da2)
{
    this.da2 = da2;
}
//------------------------------------------
public String getDa3()
{
    return this.da3;
}
public void SetDa3(String da3)
{
    this.da3 = da3;
}
//-------------------------------------------------
public String getDa4()
{
    return this.da4;
}
public void SetDa4(String da4)
{
    this.da4 = da4;
}
//-------------------------------------------------
public String getDa5()
{
    return this.da5;
}
public void SetDa5(String da5)
{
    this.da5 = da5;
}
//-------------------------------------------------
public String getDa6()
{
    return this.da6;
}
public void SetDa6(String da6)
{
    this.da6 = da6;
}
//-------------------------------------------------
public String getDa7()
{
    return this.da7;
}
public void SetDa7(String da7)
{
    this.da7 = da7;
}
//-------------------------------------------------
public String getDa8()
{
    return this.da8;
}
public void SetDa8(String da8)
{
    this.da8 = da8;
}

}

这是我的创建表方法:

    public class DataProvider extends SQLiteOpenHelper {


    // Database Version
    private static final int DATABASE_VERSION = 1;

      // Database Name
       private static final String DATABASE_NAME = "contactsManager";

      // Contacts table name
      private static final String TABLE = "contacts";

public DataProvider(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

// Contacts Table Columns names
private static final String KEY_NAME = "name";
private static final String KEY_DECRIBED = "described";
private static final String KEY_DA1 = "da1";
private static final String KEY_DA2 = "da2";
private static final String KEY_DA3 = "da3";
private static final String KEY_DA4 = "da4";
private static final String KEY_DA5 = "da5";
private static final String KEY_DA6 = "da6";
private static final String KEY_DA7 = "da7";
private static final String KEY_DA8 = "da8";




@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE + "("
             +KEY_NAME + " TEXT,"
            + KEY_DECRIBED + " TEXT" 
            + KEY_DA1 + " TEXT"
            + KEY_DA2 + " TEXT"
            + KEY_DA3 + " TEXT"
            + KEY_DA4 + " TEXT"
            + KEY_DA5 + " TEXT"
            + KEY_DA6 + " TEXT"
            + KEY_DA7 + " TEXT"
            + KEY_DA8 + " TEXT"+ ")";
    db.execSQL(CREATE_CONTACTS_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub
    db.execSQL("DROP TABLE IF EXISTS " + TABLE);

    // Create tables again
    onCreate(db);
}

public void addContact(Name contact) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_NAME, contact.getName()); // Contact Name
    values.put(KEY_DECRIBED, contact.getDescribed());
    values.put(KEY_DA1, contact.getDa1());
    values.put(KEY_DA2, contact.getDa2());
    values.put(KEY_DA3, contact.getDa3());
    values.put(KEY_DA4, contact.getDa4());
    values.put(KEY_DA5, contact.getDa5());
    values.put(KEY_DA6, contact.getDa6());
    values.put(KEY_DA7, contact.getDa7());
    values.put(KEY_DA8, contact.getDa8());
    // Contact Described

    // Inserting Row
    db.insert(TABLE, null, values);
    db.close(); // Closing database connection
}

 // Getting single contact
public Name getContact(int id) {
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.query(TABLE, new String[] {
            KEY_NAME, KEY_DECRIBED,KEY_DA1,KEY_DA2,KEY_DA3,KEY_DA4,KEY_DA5,KEY_DA6,KEY_DA7,KEY_DA8 },
            null, new String[] { String.valueOf(id) }, null, null, null, null);
    if (cursor != null)
        cursor.moveToFirst();

    Name contact = new Name(
            cursor.getString(0),
            cursor.getString(1),
            cursor.getString(2), 
            cursor.getString(3), 
            cursor.getString(4), 
            cursor.getString(5),
            cursor.getString(6), 
            cursor.getString(7), 
            cursor.getString(8), 
            cursor.getString(9));
    // return contact
    return contact;
}

public List<Name> getAllContacts() {
    List<Name> contactList = new ArrayList<Name>();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            Name contact = new Name();

            contact.SetName(cursor.getString(0));
            contact.SetDescribed(cursor.getString(1));
            contact.SetDa1(cursor.getString(2));
            contact.SetDa2(cursor.getString(3));
            contact.SetDa3(cursor.getString(4));
            contact.SetDa4(cursor.getString(5));
            contact.SetDa5(cursor.getString(6));
            contact.SetDa6(cursor.getString(7));
            contact.SetDa7(cursor.getString(8));
            contact.SetDa8(cursor.getString(9));

            // Adding contact to list
            contactList.add(contact);
        } while (cursor.moveToNext());
    }

    // return contact list
    return contactList;
}

// Getting contacts Count
public int getContactsCount() {
    String countQuery = "SELECT  * FROM " + TABLE;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(countQuery, null);
    cursor.close();

    // return count
    return cursor.getCount();
}
}

这是Logcat:

03-05 18:31:45.575: D/dalvikvm(378): GC_FOR_ALLOC freed 36K, 5% free 6323K/6595K,     paused 81ms
03-05 18:31:45.615: I/dalvikvm-heap(378): Grow heap (frag case) to 7.696MB for 1536016-    byte allocation
03-05 18:31:45.716: D/dalvikvm(378): GC_FOR_ALLOC freed <1K, 4% free 7823K/8135K,     paused 74ms
03-05 18:31:45.875: D/dalvikvm(378): GC_CONCURRENT freed <1K, 4% free 7823K/8135K,     paused 5ms+3ms
03-05 18:31:46.145: D/dalvikvm(378): GC_FOR_ALLOC freed <1K, 4% free 7824K/8135K, paused 63ms
03-05 18:31:46.165: I/dalvikvm-heap(378): Grow heap (frag case) to 8.347MB for 682256- byte allocation
03-05 18:31:46.265: D/dalvikvm(378): GC_FOR_ALLOC freed 0K, 4% free 8490K/8839K, paused 89ms
03-05 18:31:46.545: D/dalvikvm(378): GC_CONCURRENT freed 1538K, 21% free 7014K/8839K, paused 5ms+6ms
03-05 18:31:46.665: D/dalvikvm(378): GC_FOR_ALLOC freed 52K, 20% free 7087K/8839K, paused 61ms
03-05 18:31:46.885: D/dalvikvm(378): GC_CONCURRENT freed <1K, 15% free 7588K/8839K, paused 12ms+4ms
03-05 18:31:46.975: W/fbzvbx(378): afgsfbvzvbaergafbz
03-05 18:31:46.985: W/fbzvbx(378): afgsfbvzvbaergafbz1
03-05 18:31:46.985: W/fbzvbx(378): afgsfbvzvbaergafbz2
03-05 18:31:47.135: I/SqliteDatabaseCpp(378): sqlite returned: error code = 1, msg =  table contacts has no column named da7,  db=/data/data/com.djcharon.Locker/databases/contactsManager
03-05 18:31:47.155: E/SQLiteDatabase(378): Error inserting da7=l da8=; da5=j da6=k name=a da3=g da4=h da1=d da2=f described=s
03-05 18:31:47.155: E/SQLiteDatabase(378): android.database.sqlite.SQLiteException: table contacts has no column named da7: , while compiling: INSERT INTO contacts(da7,da8,da5,da6,name,da3,da4,da1,da2,described) VALUES (?,?,?,?,?,?,?,?,?,?)
03-05 18:31:47.155: E/SQLiteDatabase(378):  at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
03-05 18:31:47.155: E/SQLiteDatabase(378):  at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64)
03-05 18:31:47.155: E/SQLiteDatabase(378):  at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:146)
03-05 18:31:47.155: E/SQLiteDatabase(378):  at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:367)
03-05 18:31:47.155: E/SQLiteDatabase(378):  at android.database.sqlite.SQLiteStatement.acquireAndLock(SQLiteStatement.java:253)
03-05 18:31:47.155: E/SQLiteDatabase(378):  at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:111)
03-05 18:31:47.155: E/SQLiteDatabase(378):  at  android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1737)
03-05 18:31:47.155: E/SQLiteDatabase(378):  at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1610)
03-05 18:31:47.155: E/SQLiteDatabase(378):  at com.djcharon.Locker.DataProvider.addContact(DataProvider.java:87)
03-05 18:31:47.155: E/SQLiteDatabase(378):  at com.djcharon.Locker.LockScreen.onCreate(LockScreen.java:108)
03-05 18:31:47.155: E/SQLiteDatabase(378):  at android.app.Activity.performCreate(Activity.java:4397)
03-05 18:31:47.155: E/SQLiteDatabase(378):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
03-05 18:31:47.155: E/SQLiteDatabase(378):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1779)
03-05 18:31:47.155: E/SQLiteDatabase(378):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831)
03-05 18:31:47.155: E/SQLiteDatabase(378):  at android.app.ActivityThread.access$500(ActivityThread.java:122)
03-05 18:31:47.155: E/SQLiteDatabase(378):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024)
03-05 18:31:47.155: E/SQLiteDatabase(378):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-05 18:31:47.155: E/SQLiteDatabase(378):  at android.os.Looper.loop(Looper.java:132)
03-05 18:31:47.155: E/SQLiteDatabase(378):  at android.app.ActivityThread.main(ActivityThread.java:4123)
03-05 18:31:47.155: E/SQLiteDatabase(378):  at java.lang.reflect.Method.invokeNative(Native Method)
03-05 18:31:47.155: E/SQLiteDatabase(378):  at java.lang.reflect.Method.invoke(Method.java:491)
03-05 18:31:47.155: E/SQLiteDatabase(378):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
03-05 18:31:47.155: E/SQLiteDatabase(378):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
03-05 18:31:47.155: E/SQLiteDatabase(378):  at dalvik.system.NativeStart.main(Native Method)
03-05 18:31:47.225: I/SqliteDatabaseCpp(378): sqlite returned: error code = 1, msg = table contacts has no column named da7, db=/data/data/com.djcharon.Locker/databases/contactsManager
03-05 18:31:47.245: E/SQLiteDatabase(378): Error inserting da7=ly da8=;u da5=jg da6=kh name=as da3=ge da4=hu da1=df da2=ft described=sd
03-05 18:31:47.245: E/SQLiteDatabase(378): android.database.sqlite.SQLiteException: table contacts has no column named da7: , while compiling: INSERT INTO contacts(da7,da8,da5,da6,name,da3,da4,da1,da2,described) VALUES (?,?,?,?,?,?,?,?,?,?)
03-05 18:31:47.245: E/SQLiteDatabase(378):  at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
03-05 18:31:47.245: E/SQLiteDatabase(378):  at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64)
03-05 18:31:47.245: E/SQLiteDatabase(378):  at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:146)
03-05 18:31:47.245: E/SQLiteDatabase(378):  at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:367)
03-05 18:31:47.245: E/SQLiteDatabase(378):  at android.database.sqlite.SQLiteStatement.acquireAndLock(SQLiteStatement.java:253)
03-05 18:31:47.245: E/SQLiteDatabase(378):  at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:111)
03-05 18:31:47.245: E/SQLiteDatabase(378):  at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1737)
03-05 18:31:47.245: E/SQLiteDatabase(378):  at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1610)
03-05 18:31:47.245: E/SQLiteDatabase(378):  at com.djcharon.Locker.DataProvider.addContact(DataProvider.java:87)
03-05 18:31:47.245: E/SQLiteDatabase(378):  at com.djcharon.Locker.LockScreen.onCreate(LockScreen.java:109)
03-05 18:31:47.245: E/SQLiteDatabase(378):  at android.app.Activity.performCreate(Activity.java:4397)
03-05 18:31:47.245: E/SQLiteDatabase(378):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
03-05 18:31:47.245: E/SQLiteDatabase(378):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1779)
03-05 18:31:47.245: E/SQLiteDatabase(378):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831)
03-05 18:31:47.245: E/SQLiteDatabase(378):  at android.app.ActivityThread.access$500(ActivityThread.java:122)
03-05 18:31:47.245: E/SQLiteDatabase(378):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024)
03-05 18:31:47.245: E/SQLiteDatabase(378):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-05 18:31:47.245: E/SQLiteDatabase(378):  at android.os.Looper.loop(Looper.java:132)
03-05 18:31:47.245: E/SQLiteDatabase(378):  at android.app.ActivityThread.main(ActivityThread.java:4123)
03-05 18:31:47.245: E/SQLiteDatabase(378):  at java.lang.reflect.Method.invokeNative(Native Method)
03-05 18:31:47.245: E/SQLiteDatabase(378):  at java.lang.reflect.Method.invoke(Method.java:491)
03-05 18:31:47.245: E/SQLiteDatabase(378):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
03-05 18:31:47.245: E/SQLiteDatabase(378):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
03-05 18:31:47.245: E/SQLiteDatabase(378):  at dalvik.system.NativeStart.main(Native Method)
03-05 18:31:47.275: I/SqliteDatabaseCpp(378): sqlite returned: error code = 1, msg = table contacts has no column named da7, db=/data/data/com.djcharon.Locker/databases/contactsManager
03-05 18:31:47.295: E/SQLiteDatabase(378): Error inserting da7=lty da8=;yu da5=jrg da6=keh name=ads da3=gje da4=hyu da1=dfg da2=fht described=sfd
03-05 18:31:47.295: E/SQLiteDatabase(378): android.database.sqlite.SQLiteException: table contacts has no column named da7: , while compiling: INSERT INTO contacts(da7,da8,da5,da6,name,da3,da4,da1,da2,described) VALUES (?,?,?,?,?,?,?,?,?,?)
03-05 18:31:47.295: E/SQLiteDatabase(378):  at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
03-05 18:31:47.295: E/SQLiteDatabase(378):  at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64)
03-05 18:31:47.295: E/SQLiteDatabase(378):  at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:146)
03-05 18:31:47.295: E/SQLiteDatabase(378):  at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:367)
03-05 18:31:47.295: E/SQLiteDatabase(378):  at android.database.sqlite.SQLiteStatement.acquireAndLock(SQLiteStatement.java:253)
03-05 18:31:47.295: E/SQLiteDatabase(378):  at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:111)
03-05 18:31:47.295: E/SQLiteDatabase(378):  at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1737)
03-05 18:31:47.295: E/SQLiteDatabase(378):  at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1610)
03-05 18:31:47.295: E/SQLiteDatabase(378):  at com.djcharon.Locker.DataProvider.addContact(DataProvider.java:87)
03-05 18:31:47.295: E/SQLiteDatabase(378):  at com.djcharon.Locker.LockScreen.onCreate(LockScreen.java:110)
03-05 18:31:47.295: E/SQLiteDatabase(378):  at android.app.Activity.performCreate(Activity.java:4397)
03-05 18:31:47.295: E/SQLiteDatabase(378):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
03-05 18:31:47.295: E/SQLiteDatabase(378):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1779)
03-05 18:31:47.295: E/SQLiteDatabase(378):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831)
03-05 18:31:47.295: E/SQLiteDatabase(378):  at android.app.ActivityThread.access$500(ActivityThread.java:122)
03-05 18:31:47.295: E/SQLiteDatabase(378):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024)
03-05 18:31:47.295: E/SQLiteDatabase(378):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-05 18:31:47.295: E/SQLiteDatabase(378):  at android.os.Looper.loop(Looper.java:132)
03-05 18:31:47.295: E/SQLiteDatabase(378):  at     android.app.ActivityThread.main(ActivityThread.java:4123)
03-05 18:31:47.295: E/SQLiteDatabase(378):  at java.lang.reflect.Method.invokeNative(Native Method)
03-05 18:31:47.295: E/SQLiteDatabase(378):  at java.lang.reflect.Method.invoke(Method.java:491)
03-05 18:31:47.295: E/SQLiteDatabase(378):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
03-05 18:31:47.295: E/SQLiteDatabase(378):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
03-05 18:31:47.295: E/SQLiteDatabase(378):  at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:2)

您的创建表查询似乎正在创建问题 尝试:

String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE + "("
         +KEY_NAME + " TEXT,"
        + KEY_DECRIBED + " TEXT," 
        + KEY_DA1 + " TEXT,"
        + KEY_DA2 + " TEXT,"
        + KEY_DA3 + " TEXT,"
        + KEY_DA4 + " TEXT,"
        + KEY_DA5 + " TEXT,"
        + KEY_DA6 + " TEXT,"
        + KEY_DA7 + " TEXT,"
        + KEY_DA8 + " TEXT"+ ");";