2014-07-10 204 views
0

因此,我正在按照本教程:http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/,仍然似乎沒有產生任何結果。有人可以幫助我,因爲我不知道爲什麼它不起作用。謝謝!它只是給我在logcat中的錯誤!Android SQLite數據庫簡單幫助

另外我怎麼會發布這個數據到textView或ListView在另一個活動?

public class DatabaseHandler extends SQLiteOpenHelper{ 

// Database version 
private static final int DATABASE_VERSION = 1; 
// Database name 
private static final String DATABASE_NAME = "athleteProgram"; 
// Athletes table name 
private static final String TABLE_ATHLETES= "athletes"; 

// Athletes table columns names 
private static final String KEY_ID = "id"; 
private static final String KEY_NAME = "name"; 
private static final String KEY_AGE = "age"; 

DatabaseHandler(Context context) 
{ 
    super(context, DATABASE_NAME, null, DATABASE_VERSION); 
} 
// Creating table 
@Override 
public void onCreate(SQLiteDatabase db) 
{ 
    String CREATE_ATHLETES_TABLE = "CREATE TABLE " + TABLE_ATHLETES + "(" + KEY_ID + 
      " INTEGER PRIMARY KEY," + KEY_NAME + "TEXT," + KEY_AGE + " TEXT" + ")"; 
    db.execSQL(CREATE_ATHLETES_TABLE); 
} 
@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
{ 
    // Drop older table if existed 
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_ATHLETES); 

    // Create tables again 
    onCreate(db); 
} 
// All CRUD(Create, Read, Update, Delete) Operations 

// Adding athlete 
void addAthlete(Athlete athlete) 
{ 
    SQLiteDatabase db = this.getWritableDatabase(); 

    ContentValues values = new ContentValues(); 
    values.put(KEY_NAME, athlete.getName()); 
    values.put(KEY_AGE, athlete.getAge()); 

    // Inserting Row 
    db.insert(TABLE_ATHLETES, null, values); 
    db.close(); // Closing database connection 
} 
// Getting single Athlete 
public Athlete getAthlete(int id) 
{ 
    SQLiteDatabase db = this.getReadableDatabase(); 

    Cursor cursor = db.query(TABLE_ATHLETES, new String[] {KEY_ID, KEY_NAME, KEY_AGE}, KEY_ID + "=?", 
      new String[] {String.valueOf(id)}, null, null, null, null); 
    if(cursor != null) 
    { 
     cursor.moveToFirst(); 
    } 

    Athlete athlete = new Athlete(Integer.parseInt(cursor.getString(0)), 
      cursor.getString(1), cursor.getString(2)); 
    return athlete; 
} 
// Getting All Athletes 
public List<Athlete> getAllAthletes() { 
    List<Athlete> athleteList = new ArrayList<Athlete>(); 
    // Select All Query 
    String selectQuery = "SELECT * FROM " + TABLE_ATHLETES; 

    SQLiteDatabase db = this.getWritableDatabase(); 
    Cursor cursor = db.rawQuery(selectQuery, null); 

    // looping through all rows and adding to list 
    if (cursor.moveToFirst()) { 
     do { 
      Athlete athlete = new Athlete(); 
      athlete.setID(Integer.parseInt(cursor.getString(0))); 
      athlete.setName(cursor.getString(1)); 
      athlete.setAge(cursor.getString(2)); 
      // Adding contact to list 
      athleteList.add(athlete); 
     } while (cursor.moveToNext()); 
    } 

    // return athlete list 
    return athleteList; 
} 
// Updating single athlete 
public int updateAthlete(Athlete athlete) { 
    SQLiteDatabase db = this.getWritableDatabase(); 

    ContentValues values = new ContentValues(); 
    values.put(KEY_NAME, athlete.getName()); 
    values.put(KEY_AGE, athlete.getAge()); 

    // updating row 
    return db.update(TABLE_ATHLETES, values, KEY_ID + " = ?", 
      new String[] { String.valueOf(athlete.getID()) }); 
} 
// Deleting single athlete 
public void deleteAthlete(Athlete athlete) { 
    SQLiteDatabase db = this.getWritableDatabase(); 
    db.delete(TABLE_ATHLETES, KEY_ID + " = ?", 
      new String[] { String.valueOf(athlete.getID()) }); 
    db.close(); 
} 
// Getting athletes Count 
public int getAthletesCount() { 
    String countQuery = "SELECT * FROM " + TABLE_ATHLETES; 
    SQLiteDatabase db = this.getReadableDatabase(); 
    Cursor cursor = db.rawQuery(countQuery, null); 
    cursor.close(); 

    // return count 
    return cursor.getCount(); 
} 

} 





public class Athlete { 

    //private variables 
    int _id; 
    String _name; 
    String _age; 

    // Empty constructor 
    public Athlete(){ 

    } 
    // constructor 
    public Athlete(int id, String name, String age){ 
     this._id = id; 
     this._name = name; 
     this._age = age; 
    } 

    // constructor 
    public Athlete(String name, String age){ 
     this._name = name; 
     this._age = age; 
    } 
    // getting ID 
    public int getID(){ 
     return this._id; 
    } 

    // setting id 
    public void setID(int id){ 
     this._id = id; 
    } 

    // getting name 
    public String getName(){ 
     return this._name; 
    } 

    // setting name 
    public void setName(String name){ 
     this._name = name; 
    } 

    // getting age 
    public String getAge(){ 
     return this._age; 
    } 

    // setting age 
    public void setAge(String age){ 
     this._age = age; 
} 
} 

public class StartingActivity extends ListActivity { 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_starting); 


} 

// Will be called via the onClick attribute 
// of the buttons in main.xml 
public void onClick(View view) { 
    @SuppressWarnings("unchecked") 
    EditText editName = (EditText) findViewById(R.id.editName); 
    EditText editAge = (EditText) findViewById(R.id.editAge); 
    EditText editDate = (EditText) findViewById(R.id.editDate); 
    EditText editTier = (EditText) findViewById(R.id.editTier); 
    DatabaseHandler db = new DatabaseHandler(this); 

    /** 
    * CRUD Operations 
    * */ 
    // Inserting Contacts 
    Log.d("Insert: ", "Inserting .."); 
    db.addAthlete(new Athlete("Joe", "20")); 
    db.addAthlete(new Athlete(editName.getText().toString(), editAge.getText().toString())); 

    // Reading all contacts 
    Log.d("Reading: ", "Reading all contacts.."); 
    List<Athlete> athletes = db.getAllAthletes(); 

    for (Athlete ath : athletes) { 
     String log = "Id: " + ath.getID() + " ,Name: " + ath.getName() + " ,Phone: " + ath.getAge(); 
     // Writing Contacts to log 
     Log.d("Name: ", log); 

    } 
} 
} 
+0

可以顯示錯誤日誌。我想我有這個問題,但我需要錯誤日誌來確認它。 –

+0

發佈時間太長。我可以在下午通過它雖然 – user3786864

+0

這裏發佈只複製n粘貼.... –

回答

0

試一試,也許你不要插入主鍵。我插入示例輸入。

db.addAthlete(new Athlete(1,"Joe", "20")); 
//same on this line 
db.addAthlete(new Athlete(2,editName.getText().toString(), editAge.getText().toString())); 

也許它可以幫助 遺憾的英語不好

0

下創建語句有問題。缺少KEY_NAME與其類型之間的空間。

字符串CREATE_ATHLETES_TABLE = 「CREATE TABLE」 + TABLE_ATHLETES + 「(」 + KEY_ID + 「INTEGER PRIMARY KEY,」 + KEY_NAME + 「TEXT」 + KEY_AGE + 「TEXT」 + 「)」; db.execSQL(CREATE_ATHLETES_TABLE);