All right, so I’ve looked at probably 30 different SQLite tutorials and even some random discussions here about it. I’m not much of a programmer, I’ve found that if I can find someone doing something already and just tweak and give credit as needed, it can be much more efficient than just recreating the wheel. I’m sure I’m not the only one that feels this way.
In eclipse, I’m trying to make a constructor for the context of my db management class. Here’s the code in its entirety. It’s not done, but I can’t progress until an error goes away so here we go:
package com.bluej.movingbuddy;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.SQLException;
import android.util.Log;
public class MBDatabaseManager {
//Database Version
private final static int DATABASE_VERSION = 1;
//Database Name
private final static String DATABASE_NAME = "dbMovingBuddy";
//items and weights table name
private final static String tblInW = "ItemsAndWeights";
//items and weights table columns
private final static String InWID = "ID";
private final static String InWItem = "Item";
private final static String InWDesc = "Description";
private final static String InWWeightOne = "Weight1";
private final static String InWWeightTwo = "Weight2";
private final static String InWWeightThree = "Weight3";
private final static String InWWeightAvg = "WeightAvg";
//allowances table name
private final static String tblAllowances = "Allowances";
//allowances table columns
private final static String AllowancesID = "ID";
private final static String AllowancesRank = "Rank";
private final static String AllowancesWithDep = "WithDep";
private final static String AllowancesNoDep = "NoDep";
//estimator table name
private final static String tblEstimator = "Estimator";
//estimator table columns
private final static String EstimatorID = "ID";
private final static String EstimatorRoom = "Room";
private final static String EstimatorItem = "Weight";
//inventory table name
private final static String tblInventory = "Inventory";
//inventory table column
private final static String InventoryID = "ID";
private final static String InventoryItem = "Item";
private final static String InventoryWeight = "Weight";
private final static String InventoryValue = "Value";
private final static String InventoryImage1 = "Image1";
private final static String InventoryCondition = "Condition";
private final static String InventoryImage2 = "Image2";
private final static String InventoryNotes = "Notes";
private final static String InventoryMovingInstructions = "MovingInstructions";
//stunt table name
private final static String TABLE_NAME = "database_table";
//stunt table column names
private final static String TABLE_ROW_ID = "id";
private final static String TABLE_ROW_ONE = "table_row_one";
private final static String TABLE_ROW_TWO = "table_row_two";
public MBDatabaseManager(Context context) {
// TODO Auto-generated constructor stub
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
//This string is used to create the database. It should be changed to suit your needs.
//create items and weights table
String dbCreateItemsAndWeights = "create table " +
tblInW +
" (" +
InWID + " integer primary key autoincrement not null," +
InWItem + " text," +
InWDesc + " text," +
InWWeightOne + " integer," +
InWWeightTwo + " integer," +
InWWeightThree + " integer," +
InWWeightAvg + " integer" +
");";
db.execSQL(dbCreateItemsAndWeights);
//allowances table
String dbCreateAllowances = "create table " +
tblAllowances +
" (" +
AllowancesID + " integer primary key autoincrement not null," +
AllowancesRank + " text," +
AllowancesWithDep + " integer," +
AllowancesNoDep + " integer" +
");";
db.execSQL(dbCreateAllowances);
//estimator table
String dbCreateEstimator = "create table " +
tblEstimator +
" (" +
EstimatorID + " integer primary key autoincrement not null," +
EstimatorRoom + " text," +
EstimatorItem + " integer" +
");";
db.execSQL(dbCreateEstimator);
//inventory table
String dbCreateInventory = "create table " +
tblInventory +
" (" +
InventoryID + " integer primary key autoincrement not null," +
InventoryItem + " text," +
InventoryWeight + " integer," +
InventoryValue + " integer," +
InventoryImage1 + " blob," +
InventoryCondition + " text," +
InventoryImage2 + " blob," +
InventoryNotes + " text," +
InventoryMovingInstructions + " text" +
");";
db.execSQL(dbCreateInventory);
//stunt table
String newTableQueryString = "create table " +
TABLE_NAME +
" (" +
TABLE_ROW_ID + " integer primary key autoincrement not null," +
TABLE_ROW_ONE + " text," +
TABLE_ROW_TWO + " text" +
");";
db.execSQL(newTableQueryString);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + tblInW);
db.execSQL("DROP TABLE IF EXISTS " + tblAllowances);
db.execSQL("DROP TABLE IF EXISTS " + tblEstimator);
db.execSQL("DROP TABLE IF EXISTS " + tblInventory);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
// Adding a row to the database table
public void addRow(String rowStringOne, String rowStringTwo){
SQLiteDatabase db = this.getWritableDatabase();
//this is a key value pair holder used by android's SQLite functions
ContentValues values = new ContentValues();
values.put(TABLE_ROW_ONE, rowStringOne);
values.put(TABLE_ROW_TWO, rowStringTwo);
//ask the database object to insert the new data
try {
db.insert(TABLE_NAME, null, values);
} catch (Exception e) {
// TODO Auto-generated catch block
Log.e("DB ERROR", e.toString());
e.printStackTrace();
}
}
//DELETING A ROW FROM THE DATABASE TABLE
//
// This is an example of how to delete a row from a database table
// using this class. In most cases, this method probably does not need to be rewritten.
//
// @param rowID the SQLite database identifier for the row to delete.
//
public void deleteRow(long rowID){
// ask the database object to delete the row of given rowID
try {
db.delete(TABLE_NAME, TABLE_ROW_ID + "=" + rowID, null);
} catch (Exception e) {
// TODO Auto-generated catch block
Log.e("DB ERROR", e.toString());
e.printStackTrace();
}
}
//UPDATING A ROW IN THE DATABASE TABLE
//
// This is an example of how to update a row in the database table
// using this class. You should edit this method to suit your needs.
//
// @param rowID the SQLite database identifier for the row to update.
// @param rowStringOne the new value for the row's first column
// @param rowStringTwo the new value for the row's second column
public void updateRow(long rowID, String rowStringOne, String rowStringTwo){
//this is a key value pair holder used by android's SQLite functions
ContentValues values = new ContentValues();
values.put(TABLE_ROW_ONE, rowStringOne);
values.put(TABLE_ROW_TWO, rowStringTwo);
//ask the database object to update the database row of given rowID
try {
db.update(TABLE_NAME, values, TABLE_ROW_ID + "=" + rowID, null);
} catch (Exception e) {
// TODO Auto-generated catch block
Log.e("DB Error", e.toString());
e.printStackTrace();
}
}
//RETRIEVING A ROW IN THE DATABASE TABLE
//'
// This is an example of how to retrieve a row from a database table using this class. You should edit this method to suit your needs.
//
// @param rowID the id of the row to retrieve
// @return an array containing the data from the row
public ArrayList<Object> getRowAsArray(long rowID){
//create an array list to store data from the database row.
//I would recommend creating a JavaBean compliant object
//to store this data instead. That way you can ensure data types are correct.
ArrayList<Object> rowArray = new ArrayList<Object>();
Cursor cursor;
try {
// this is a database call that creates a "cursor" object.
// the cursor object stores the information collected from the
// database and is used to iterate through the data.
cursor = db.query(
TABLE_NAME,
new String[] { TABLE_ROW_ID, TABLE_ROW_ONE, TABLE_ROW_TWO },
TABLE_ROW_ID + "=" + rowID,
null, null, null, null, null);
//move the pointer to position zero in the cursor.
cursor.moveToFirst();
// if there is data available after the cursor's pointer, add
// it to the ArrayList that will be returned by the method.
if (!cursor.isAfterLast()){
do{
rowArray.add(cursor.getLong(0));
rowArray.add(cursor.getString(1));
rowArray.add(cursor.getString(2));
} while (cursor.moveToNext());
}
//let java know that you are through with the cursor.
cursor.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
Log.e("DB ERROR", e.toString());
e.printStackTrace();
}
//return the ArrayList containing the given row from the database.
return rowArray;
}
//RETRIEVING ALL ROWS FROM THE DATABASE TABLE
//
//This is an example of how to retrieve all data from a database table using this class.
//You should edit this method to suit your needs.
//
// the key is automatically assigned by the database
public ArrayList<ArrayList<Object>> getAllRowsAsArrays(){
//create an ArrayList that will hold all of the data collected from the database
ArrayList<ArrayList<Object>> dataArrays = new ArrayList<ArrayList<Object>>();
//this is a database call that creates a "cursor" object.
//the cursor object stores the information collected from the database and is used to iterate through the data.
Cursor cursor;
try{
//ask the database object to create the cursor.
cursor = db.query(
TABLE_NAME,
new String[]{TABLE_ROW_ID, TABLE_ROW_ONE, TABLE_ROW_TWO},
null, null, null, null, null
);
//move the cursor's pointer to position zero.
cursor.moveToFirst();
//if there is data after the current cursor position add it to the ArrayList.
if (!cursor.isAfterLast()){
do
{
ArrayList<Object> dataList = new ArrayList<Object>();
dataList.add(cursor.getLong(0));
dataList.add(cursor.getString(1));
dataList.add(cursor.getString(2));
dataArrays.add(dataList);
}
//move the cursor's pointer up one position.
while (cursor.moveToNext());
}
}
catch (SQLException e){
Log.e("DB ERROR", e.toString());
e.printStackTrace();
}
//return the ArrayList that holds the data collected from the database.
return dataArrays;
}
}
So I’ve got two errors now that I’m looking at it again. When I make the constructor
public MBDatabaseManager(Context context) {
// TODO Auto-generated constructor stub
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
Eclipse freaks out and says “The constructor object is undefined” and when I use eclipse to fix it, it removes all the context stuff and becomes super();. I don’t understand everything that well yet, but I’m pretty sure that’s gonna get me nowhere quick.
Also when I try to
SQLiteDatabase db = this.getWritableDatabase();
It also gives me an error, saying “The method getWritableDatabase() is undefined for the type MBDatabaseManager”
I don’t know what I’m missing guys. It’s something obvious I’m sure. Please assist. I need a second or third set of eyes on this.
Thanks much.
You forgot to extend a base class, for instance
public class MBDatabaseManager extends SQLiteOpenHelper.The super constructor you try to call is
public SQLiteOpenHelper (Context context, String name, SQLiteDatabase.CursorFactory factory, int version)and your call is correct and will work after inheritingSQLiteOpenHelper.