无法在android sqlLite3数据库中存储facebook访问令牌

时间:2011-08-16 08:31:39

标签: android sqlite facebook-graph-api

我正在制作涉及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();

在数据库中插入值的代码: -

public class 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);

    }
}

}

4 个答案:

答案 0 :(得分: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);
    }

答案 1 :(得分:0)

直到我们看到您的表格方案,我们无法提供有关可能导致错误的信息。也许你在创建表时限制 acces_token 长度?

答案 2 :(得分:0)

根据我的经验,无需存储访问令牌。有一些很好的理由:

(1)Facebook API具有Singal-Sign-On功能,可以完全满足您的需求

(2)访问令牌可以更改,这不会发生在每天或每周,但确实如此,当它发生时,您将需要在登录失败后更新您的数据库。

(3)该API适用于Android的官方Facebook应用程序,因此如果用户在手机上的Facebook应用程序中登录,则无需在您的应用程序中使用。

(4)如果用户未在手机上安装官方Facebook应用程序,则可以使用浏览器中的cookie,这可以通过Facebook API自动完成。

答案 3 :(得分: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”表),放宽对其他字段的约束,或者插入整行数据而不仅仅是{{ 1}}。