-1

我创建了 2 个表,第一个表工作正常,但第二个表在插入日志中显示的错误时不工作 cat no such table 我想知道为什么没有创建联系人表

public class DBAdapter {

    public static final String KEY_USERNAME="username";
    public static final String KEY_PASSWORD="password";
    public static final String KEY_ID="id";
    public static final String KEY_NAME="name";
    public static final String KEY_PHONE="phone";
    public static final String KEY_EMAIL="e_mail";
    public static final String TABLE_USERS="users";
    public static final String TABLE_CONTACTS="contacts";
    public static final String DATABASE_NAME="project1";
    public static final int DATABSE_VERSION=1;
/////////////////////////////////////////////////
    public String KEY_NAME()
    {
        return KEY_NAME;
    }
    public String KEY_PHONE()
    {
        return KEY_PHONE;
    }
    public String KEY_EMAIL()
    {
        return KEY_EMAIL;
    }
/////////////////////////////////////////////////       
    private Context c;
    private SQLiteDatabase db;
    private DBHelper helper;
/////////////////////////////////////////////////       
    public DBAdapter(Context ctx) {
        // TODO Auto-generated constructor stub
        c=ctx;
        helper=new DBHelper(c);
    }
/////////////////////////////////////////////////
private static class DBHelper extends SQLiteOpenHelper
{
    public DBHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABSE_VERSION);
            // TODO Auto-generated constructor stub
        }

@Override
public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
            try{
        db.execSQL("create table if not exists 
                "+TABLE_USERS+" ("+KEY_USERNAME+" text primary key,"+KEY_PASSWORD+" 
                text not null);");
        db.execSQL("create table if not exists "+TABLE_CONTACTS+"("+KEY_ID+" 
                integer autoincrement primary key,"+KEY_NAME+" text not 
                null,"+KEY_PHONE+" integer not null,"+KEY_EMAIL+" text  
                ,"+KEY_USERNAME+" text not null foriegn key);");

    }
            catch (SQLException e) {
                // TODO: handle exception
                Log.e(TABLE_USERS, "table is not created");
            }
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion,
                int newVersion) {
            // TODO Auto-generated method stub
            db.execSQL("DROP TABLE IF EXISTS "+TABLE_USERS);
            db.execSQL("DROP TABLE IF EXISTS "+TABLE_CONTACTS);
            onCreate(db);
        }
    }
/////////////////////////////////////////////////////
    public void open()
    {
        try{
            db=helper.getWritableDatabase();
        }
        catch (SQLiteException e) {
            // TODO: handle exception
        }
    }
/////////////////////////////////////////////////////
    public void close()
    {
        helper.close();
    }
////////////////////////////////////////////////////
    public long register_user(String uname,String pass)
    {
        ContentValues values=new ContentValues();
        values.put(KEY_USERNAME, uname);
        values.put(KEY_PASSWORD, pass);
        return db.insert(TABLE_USERS, null, values);
    }
////////////////////////////////////////////////////
    public boolean login(String uname,String pass)
    {
        Cursor c=db.rawQuery("select * from "+TABLE_USERS+" where  
                    "+KEY_USERNAME+"= ?",new String[]{uname});
        if(c.moveToFirst())
        {

                           if(c.getString(c.getColumnIndex(KEY_PASSWORD)).equals(pass))
            {
                return true;
            }
            else
                return false;
        }
        else
        {
            return false;
        }
    }
////////////////////////////////////////////////////
    public long addcontact(String name,String phone,String email,String user)
    {
        ContentValues value=new ContentValues();
        value.put(KEY_NAME, name);
        value.put(KEY_PHONE, phone);
        value.put(KEY_EMAIL, email);
        value.put(KEY_USERNAME, user);
        return db.insert(TABLE_CONTACTS, null, value);
    }
////////////////////////////////////////////////////    
/*      public long addcontact(String name,String phone,String user)
    {
        ContentValues value=new ContentValues();
        value.put(KEY_NAME, name);
        value.put(KEY_PHONE, phone);
        value.put(KEY_USERNAME, user);
        return db.insert(TABLE_CONTACTS, null, value);
    }*/
////////////////////////////////////////////////////
    public Cursor show_all_contacts(String user)
    {
        Cursor c=db.rawQuery("select * from "+TABLE_CONTACTS+" where                   
                    "+KEY_USERNAME+"= ?", new String[]{user});
        return c;
    }
////////////////////////////////////////////////////

}
4

1 回答 1

0
KEY_USERNAME+" text not null foriegn key);"

您必须得到table is not created消息,因为您没有正确拼写“foreign”。(无论如何,这不是外键的正确语法)。

于 2013-11-11T21:12:03.133 回答