Can anyone alter this code to include one integer variable. Not including the database version integer of course. 🙂
package T2T.Game;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DatabaseUtil {
private static final String TAG = "DatabaseUtil";
/**
* Database Name
*/
private static final String DATABASE_NAME = "t2tvariables_database";
/**
* Database Version
*/
private static final int DATABASE_VERSION = 1;
/**
* Table Name
*/
private static final String DATABASE_TABLE = "tb_variable";
/**
* Table columns
*/
public static final String KEY_VARIABLE = "variable";
public static final String KEY_VALUE = "grade";
public static final String KEY_ROWID = "_id";
/**
* Database creation sql statement
*/
private static final String CREATE_VARIABLE_TABLE =
"create table " + DATABASE_TABLE + " (" + KEY_ROWID + " integer primary key autoincrement, "
+ KEY_VARIABLE +" text not null, " + KEY_VALUE + " text not null);";
/**
* Context
*/
private final Context mCtx;
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
/**
* Inner private class. Database Helper class for creating and updating database.
*/
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
/**
* onCreate method is called for the 1st time when database doesn't exists.
*/
@Override
public void onCreate(SQLiteDatabase db) {
Log.i(TAG, "Creating DataBase: " + CREATE_VARIABLE_TABLE);
db.execSQL(CREATE_VARIABLE_TABLE);
}
/**
* onUpgrade method is called when database version changes.
*/
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion);
}
}
/**
* Constructor - takes the context to allow the database to be
* opened/created
*
* @param ctx the Context within which to work
*/
public DatabaseUtil(Context ctx) {
this.mCtx = ctx;
}
/**
* This method is used for creating/opening connection
* @return instance of DatabaseUtil
* @throws SQLException
*/
public DatabaseUtil open() throws SQLException {
mDbHelper = new DatabaseHelper(mCtx);
mDb = mDbHelper.getWritableDatabase();
return this;
}
/**
* This method is used for closing the connection.
*/
public void close() {
mDbHelper.close();
}
/**
* This method is used to create/insert new record Variable record.
* @param variable
* @param integer
* @return long
*/
public long createVariable(String variable, String grade ) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_VARIABLE, variable);
initialValues.put(KEY_VALUE, grade);
return mDb.insert(DATABASE_TABLE, null, initialValues);
}
/**
* This method will delete Variable record.
* @param rowId
* @return boolean
*/
public boolean deleteVariable(long rowId) {
return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
}
/**
* This method will return Cursor holding all the Variable records.
* @return Cursor
*/
public Cursor fetchAllVariables() {
return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_VARIABLE,
KEY_VALUE}, null, null, null, null, null);
}
/**
* This method will return Cursor holding the specific Variable record.
* @param id
* @return Cursor
* @throws SQLException
*/
public Cursor fetchVariable(long id) throws SQLException {
Cursor mCursor =
mDb.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
KEY_VARIABLE, KEY_VALUE}, KEY_ROWID + "=" + id, null,
null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
/**
* This method will update Variable record.
* @param id
* @param variable
* @param standard
* @return boolean
*/
public boolean updateVariable(int id, String variable, String standard) {
ContentValues args = new ContentValues();
args.put(KEY_VARIABLE, variable);
args.put(KEY_VALUE, standard);
return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + id, null) > 0;
}
}
You can put any type of value into any type of column in sqlite databse.. except that if you try to put anything other than an integer into an INTEGER PRIMARY KEY column you get an error.. so you should not have any problem inserting integers into database… while you are retrieving the values.. you can type cast them into required type.. as integers in this case…