2012-08-25 50 views
0

好的,所以我查看了大概30個不同的SQLite教程,甚至在這裏討論了一些關於它的隨機討論。我不是一個程序員,我發現如果我能夠找到某人已經做了某些事情,並根據需要進行調整並給予獎勵,那麼它可以比重新創建輪子更高效。我確信我不是唯一一個這樣認爲的人。sqlite android基礎知識...我錯過了什麼?

在eclipse中,我試圖爲我的數據庫管理類的上下文做一個構造函數。這是完整的代碼。它沒有這樣做,但我不能取得進展,直到錯誤消失所以在這裏,我們去:

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; 
} 

} 

現在我又看着它,所以我有兩個錯誤。當我做構造

public MBDatabaseManager(Context context) { 
    // TODO Auto-generated constructor stub 
    super(context, DATABASE_NAME, null, DATABASE_VERSION); 
} 

Eclipse的怪胎,說「構造的對象是不確定的」,當我使用Eclipse來修復它,它會刪除所有上下文的東西,變成超();.我不明白這一切,但我很確定這會讓我無法快速完成任務。

而且當我嘗試

SQLiteDatabase db = this.getWritableDatabase(); 

這也給了我一個錯誤,說「的方法getWritableDatabase()是未定義的類型MBDatabaseManager」

我不知道我是什麼失蹤的傢伙。這很明顯,我確定。請協助。我需要第二或第三組眼睛。

非常感謝。

+1

MBDatabaseManager類沒有超類或沒有擴展任何類 – Ramprasad

回答

0

您忘記擴展基類,例如public class MBDatabaseManager extends SQLiteOpenHelper

你試圖打電話的超級構造函數是public SQLiteOpenHelper (Context context, String name, SQLiteDatabase.CursorFactory factory, int version),你的調用是正確的,並且在繼承SQLiteOpenHelper之後會起作用。

+0

哎呀!/highfive拉茲優秀的發現。我是一個noobcakes。 – bluej

0

沒有超級(Object)考慮你通過的參數,也沒有方法叫​​。

您是否想要子類SQLiteOpenHelper