2016-04-21 67 views
-1
import android.content.*; 
    import android.database.*; 
    import android.database.sqlite.SQLiteDatabase; 
    import android.database.sqlite.SQLiteOpenHelper; 
    import android.util.Log; 

    public class DBAdapter extends SQLiteOpenHelper { 



private static final int DATA_VERSION = 1; 
private static final String DATABASE_NAME = "database.db"; 
public static final String TABLE = "table"; 
public static final String COLUMN_CONDUCTIVITY ="Conductivity"; 
public static final String COLUMN_OXYGEN = "Oxygen"; 
public static final String COLUMN_PH = "pH"; 
public static final String COLUMN_USERNAME = "username"; 
public static final String COLUMN_PASSWORD = "password"; 
public static final String COLUMN_MOISTURE = "moisture"; 

public DBAdapter(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) { 
    super(context, DATABASE_NAME, factory, DATA_VERSION); 
} 

public DBAdapter(Context context, String name, SQLiteDatabase.CursorFactory factory, int version, DatabaseErrorHandler errorHandler) { 
    super(context, name, factory, version, errorHandler); 
} 

@Override 
public void onCreate(SQLiteDatabase db) { 

    String query = "CREATE TABLE" + TABLE + "(" + 
      COLUMN_CONDUCTIVITY+ " FLOAT" + 
      COLUMN_OXYGEN+ " INTEGER" + 
      COLUMN_PH+ "FLOAT " + 
      COLUMN_USERNAME+ "STRING PRIMARY KEY " + 
      COLUMN_PASSWORD + "TEXT" + 
      COLUMN_MOISTURE+ " INTEGER "+ 
      ");"; 

    db.execSQL(query); 
} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    db.execSQL("DROP_TABLE_IF_EXITS" + TABLE); 
    onCreate(db); 
} 
//Add a new row to the database 
public void addData(DBAdapter data){ 
    ContentValues values = new ContentValues(); 
    values.put(COLUMN_CONDUCTIVITY, data.getConductivity 

    values.put(COLUMN_OXYGEN, data.getOxygen()); 
    values.put(COLUMN_PASSWORD, data.getPassword()); 
    values.put(COLUMN_USERNAME, data.getUsername()); 
    values.put(COLUMN_PH, data.getPh()); 

    SQLiteDatabase db = getWritableDatabase(); 
    db.insert(TABLE, null, values); 
    db.close(); 
} 
//Delete data 

public void deleteData(){ 
    SQLiteDatabase db = getWritableDatabase(); 
    db.execSQL("DROP TABLE IF EXIST, TABLE"); 
} 
public String databaseToString(){ 

    String dbString = ""; 
    SQLiteDatabase db = getWritableDatabase(); 
    String query = "SELECT * FROM" + TABLE + "WHERE 1"; 
    Cursor c = db.rawQuery(query,null); 
    c.moveToFirst(); 
    while(!c.isAfterLast()){ 
     if(c.getString(c.getColumnIndex("Conductivity"))!= null){ 
      dbString += c.getString(c.getColumnIndex(("Conductivity"))); 
      dbString += "\n"; 
     } 
    } 
    db.close(); 
    return dbString; 

} 

}爲什麼不檢測Get方法?

我上面的代碼沒有檢測從下面的代碼的get方法,是的,他們是在同一個項目。任何獲得者都可以找到。

任何人都可以幫助嗎?

請我試着用這個SQLite數據庫來工作。

public class DatabaseList { 



    private String username; 
    private String password; 
    private float conductivity; 
    private float pH; 
    private int oxygen; 
    private String date; 

    public DatabaseList(){} 

    public DatabaseList(String username, String password, String date, int oxygen, float pH, float conductivity){ 
     this.conductivity = conductivity; 
     this.date = date; 
     this.oxygen = oxygen; 
     this.password = password; 
     this.pH = pH; 
     this.username = username; 
    } 

    public String getUsername() { 
     return username; 
    } 

    public void setUsername(String username) { 
     this.username = username; 
    } 

    public String getPassword() { 
     return password; 
    } 

    public void setPassword(String password) { 
     this.password = password; 
    } 

    public float getConductivity() { 
     return conductivity; 
    } 

    public void setConductivity(float conductivity) { 
     this.conductivity = conductivity; 
    } 

    public float getpH() { 
     return pH; 
    } 

    public void setpH(float pH) { 
     this.pH = pH; 
    } 

    public int getOxygen() { 
     return oxygen; 
    } 

    public void setOxygen(int oxygen) { 
     this.oxygen = oxygen; 
    } 

    public String getDate() { 
     return date; 
    } 

    public void setDate(String date) { 
     this.date = date; 
    } 
} 
+2

當您需要'DatabaseList'來訪問getter和setter時,您有'DBAdapter data' – Raghunandan

回答

1

好了,將對DBAdapter類和數據庫列表,甚至沒有在相同類型的類, 有將對DBAdapter類

getOxygen() 

方法?我不這麼認爲... 所以這就是錯誤 您正嘗試調用另一個類中未定義的方法..

相關問題