我的数据库不会插入

时间:2012-01-16 13:04:02

标签: android

我在另一个应用程序中完成了同样的事情并且工作正常。但是从我的应用程序中获取所需的代码并没有成功。无论我看多少次,我都看不出我做错了什么。我是这里的某个人可以指出我出错的地方。

sqlite db adapter

public class triDbAdapter {




private static final String TAG = "dbAdapter";
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
private final Context mCtx;

private static class DatabaseHelper extends SQLiteOpenHelper {

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



    @Override
    public void onCreate(SQLiteDatabase db) {

        db.execSQL(DATABASE_CREATE1);
        db.execSQL(DATABASE_CREATE2);
        db.execSQL(DATABASE_CREATE3);


    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                + newVersion + ". ALL data will be destroyed");
        db.execSQL("DROP TABLE IF EXISTS user");
        db.execSQL("DROP TABLE IF EXISTS games");
        db.execSQL("DROP TABLE IF EXISTS tilesPlayed");
        onCreate(db); 

    }

}    

/**
 * Constructor - takes the context to allow the database to be
*/
public  triDbAdapter(Context ctx) {
    this.mCtx = ctx;
}

/**
 * 
 */
public triDbAdapter open() throws SQLException {
    mDbHelper = new DatabaseHelper(mCtx);
    mDb = mDbHelper.getWritableDatabase();
    return this;
}

public void close() {
    mDbHelper.close();
}

/**
 * createUserProfile inserts the local username and server-generated userid, _ID , into the database.
 */

public long createUserProfile(String username, int _id) {
    ContentValues initialValues = new ContentValues();
    initialValues.put(KEY_NAME, username);
    initialValues.put(KEY_USERID, _id);

    return mDb.insert(PLAYER_TABLE, null, initialValues);
}

活动

public class game extends Activity {

private EditText mNameText;  // username
private String userName;
private int userId;
private Context mCtx;
private ServerCommunication sComm;
private Boolean good_to_go;
private triDbAdapter mDbHelper;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    mCtx = this;
    mDbHelper = new triDbAdapter(this);
    mDbHelper.open();

    sComm = new ServerCommunication();
    good_to_go = false;

    setContentView(R.layout.initial_form);

    mNameText = (EditText) findViewById(R.id.username);
    Button submitButton = (Button) findViewById(R.id.submit);
    submitButton.setOnClickListener(doIt);


}

private OnClickListener doIt = new OnClickListener() {
    public void onClick(View v) {

        userName = mNameText.getText().toString();
        if (userName.isEmpty()) {
            Toast.makeText(mCtx, "need a name here.", Toast.LENGTH_SHORT).show();
        }
        else {
            try {
                userId = sComm.registerUser(mCtx,userName);
                if (userId > 0) {
                    // returns true - we're good to go!
                    Toast.makeText(mCtx, "Congratulations! You're ready to play Tri-Ominoes", Toast.LENGTH_SHORT).show();
                    good_to_go = true;
                    Long ret = mDbHelper.createUserProfile(userName,userId);
                    if (ret == 0) {
                        //
                    }
                }
                else {
                    // the only error we could get is duplicate name
                    Toast.makeText(mCtx, "Sorry, username:" + userName +" already exists. Try another name.", Toast.LENGTH_SHORT).show();
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

};

}

我从edittext获取用户名然后调用HTTP函数来解析从服务器返回的一些JSON数据。 这一切都回来了,但是当我尝试将其插入数据库并创建时 和用户配置文件失败。数据库可用并创建 - 我可以将它从模拟器中取出并使用Sqlite数据库读取它,但表格在那里,只是空的。

我在这里做错了什么想法?我有一种感觉,这是一种简单而真实的前额拍打。如果是的话,请随意虐待我。 :)用户

1 个答案:

答案 0 :(得分:0)

插入所有列有帮助,或在允许的位置插入空值。

相关问题