2011-08-16 30 views
0

我正在做一個涉及Facebook整合的應用程序。 我必須存儲訪問令牌,以便它不需要再次登錄以查看應用程序。但我無法存儲訪問令牌。 以下是錯誤,我得到在Android的sqlLite3數據庫坎特商店Facebook訪問令牌

08-16 13:57:53.236: ERROR/Database(7964): Error inserting acces_token=177852938929775|8e4aec98e182f7034b497766.3-618306968|YAriJqaRTDz1aIgNHjom_tdBnnw 
08-16 13:57:53.236: ERROR/Database(7964): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed 
08-16 13:57:53.236: ERROR/Database(7964):  at android.database.sqlite.SQLiteStatement.native_execute(Native Method) 
08-16 13:57:53.236: ERROR/Database(7964):  at android.database.sqlite.SQLiteStatement.execute(SQLiteStatement.java:55) 
08-16 13:57:53.236: ERROR/Database(7964):  at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1623) 
08-16 13:57:53.236: ERROR/Database(7964):  at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1484) 
08-16 13:57:53.236: ERROR/Database(7964):  at org.db.dbHelper.insertRows(dbHelper.java:63) 
08-16 13:57:53.236: ERROR/Database(7964):  at org.neighbourhood.neighbourhood.onFacebookConnectorWorking(neighbourhood.java:142) 

代碼: -

dbHelper db = new dbHelper(neighbourhood.this); 
     db.open(); 
     ContentValues cv = new ContentValues(); 
     cv.put("acces_token", 
        facebookConnector.getFacebook().getAccessToken()); 
     db.insertRows(cv, "facebook"); 
     db.close(); 

守則inset值在數據庫: -

公共類dbHelper {

private final String DATABASE_NAME = "neighbourhood.db"; 
private final String TAG = "dbHelper"; 

private static final String DATABASE_CREATE_TABLE_LOGIN = "create table login" + 
     " (is_login text not null);"; 

private static final String DATABASE_CREATE_TABLE_FACEBOOK = "create table " 
     + "facebook" 
     + " (name text not null," 
     + "first_name text not null," + 
     "last_name text not null," + 
     "gender text not null," + 
     "email text not null," + 
     "id text not null," + 
     "birthday text not null," + 
     "acces_token text not null);"; 


private SQLiteDatabase myDatabase; 
private myDbhelper helper; 

public dbHelper(Context context) { 
    helper = new myDbhelper(context, DATABASE_NAME, null, 1); 
} 

/** 
* This method is used to open the database in Writable mode 
* 
* @return instance of the database 
*/ 
public dbHelper open() { 
    try { 
     myDatabase = helper.getWritableDatabase(); 
    } catch (SQLException ex) { 
     ex.printStackTrace(); 
     } 
    return this; 
} 

/** 
* This method is used for closing the database 
*/ 
public void close() { 
    myDatabase.close(); 
} 


public long insertRows(ContentValues values, String tableName) { 
    long val = myDatabase.insert(tableName, null, values); 
    return val; 
} 

public Cursor getAllValues(String tableName) { 
    Cursor myResult; 
    myResult = myDatabase.query(tableName, null, null, null, null, null, 
      null, null); 

    return myResult; 
} 


private static class myDbhelper extends SQLiteOpenHelper { 
    public myDbhelper(Context context, String name, CursorFactory factory, 
      int version) { 
     super(context, name, factory, version); 
    } 

    /** 
    * this method is called when the database is created first time 
    */ 
    @Override 
    public void onCreate(SQLiteDatabase _db) { 
     _db.execSQL(DATABASE_CREATE_TABLE_LOGIN); 
     _db.execSQL(DATABASE_CREATE_TABLE_FACEBOOK); 

    } 

    /** 
    * this method is called when the database version is changed 
    */ 
    @Override 
    public void onUpgrade(SQLiteDatabase _db, int _oldVersion, 
      int _newVersion) { 
     /* 
     * _db.execSQL("DROP TABLE IF EXISTS all_audio"); 
     * _db.execSQL("DROP TABLE IF EXISTS all_video"); 
     * _db.execSQL("DROP TABLE IF EXISTS all_playlist"); 
     */ 
     _db.execSQL("DROP TABLE IF EXISTS all_playlist_song"); 
     onCreate(_db); 

    } 
} 

}

+0

你的數據庫看起來像什麼?什麼是dbHelper.java中的第63行? –

+0

您的logcat錯誤顯示違反約束 - 您可以添加用於創建數據庫表的代碼到您的問題嗎? – Pikaling

回答

0

你的架構指定您facebook表的所有字段應該是NOT NULL,但你只插入acces_token領域,這將迫使剩餘的字段是NULL

private static final String DATABASE_CREATE_TABLE_FACEBOOK = "create table " 
    + "facebook" 
    + " (name text not null," 
    + "first_name text not null," + 
    "last_name text not null," + 
    "gender text not null," + 
    "email text not null," + 
    "id text not null," + 
    "birthday text not null," + 
    "acces_token text not null);"; 

這就是爲什麼例外說android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed - 你NOT NULL約束受到侵犯。

您需要將acces_token分隔成一個單獨的表格(例如假設的「admin」表格),放寬您對其他字段的約束,或者插入整行數據而不僅僅是acces_token

+0

Alrighty然後:) – abhishek

1

insertRows方法應該具有ContentValues參數。 而在那個方法中,你必須調用contentvalues.putall方法來在數據庫中插入值。 試試這個:

public long insertRows(ContentValues values, String tableName) 
    { 
     ContentValues in=new ContentValues(); 
     in.putAll(values); 


     return myDatabase.insert(tableName, null, in); 
    } 
+0

這是我的插入行方法 - >公共長插入行(ContentValues值,字符串表名){ \t \t long val = myDatabase.insert(tableName,null,values); \t \t return val; \t} – abhishek

+0

shudnt it in.putAll(values)??並不是我的相同的代碼? – abhishek

+0

現在你可以試試 –

0

直到我們看到你的表方案,我們不能提供什麼可能導致錯誤的信息。也許你限制acces_token創建表時的長度?

0

根據我的經驗,沒有必要存儲訪問令牌。有一些很好的理由:

(1)Facebook的API有葛-登錄功能,你想要做什麼

(2)訪問令牌可以改變的,這不會發生每天或每一週,但確實如此,並且當它發生時,您將需要在登錄失敗後更新數據庫。 (3)該API與Android的官方Facebook應用程序一起使用,所以如果用戶在他們的手機上登錄Facebook應用程序,他們將不必在您的應用程序中。 (4)如果用戶沒有在他們的電話上安裝官方Facebook應用程序,則可以使用來自瀏覽器的cookie,並且這是通過Facebook API自動完成的。