创建数据库连接和创建数据库表时遇到问题

时间:2014-02-21 16:36:21

标签: java android android-sqlite

我有两个一级是MainActivity.java和DBadapter.java 主要活动看起来像这个代码:

public class MainActivity extends Activity {
DBadapter myDB;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_activity);
openDB();
populateListFromDB();
}
 private void onDistroy(){
 super.onDestroy();
 closeDB();
 }

 private void openDB(){
 myDB = new DBadapter(this);
 myDB.open();
 }
 public void closeDB(){
myDB.close();
}


private void populateListFromDB(){
Cursor cursor = myDB.getAllRows();
startManagingCursor(cursor);

String[] columnFromTable = new String[]
        {DBadapter.KEY_NAME, DBadapter.KEY_GAMES, DBadapter.KEY_WON, 
        DBadapter.KEY_LOST, DBadapter.KEY_DIFF, DBadapter.KEY_POINTS};

int[] toLayoutElement = new int[]
        {R.id.txtview_name, R.id.txtview_gameno, R.id.txtview_won, 
        R.id.txtview_lost, R.id.txtview_difference, R.id.txtview_points};


SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(
        this, R.layout.item_layout, cursor, columnFromTable, toLayoutElement);

ListView mylistview = (ListView) findViewById(R.id.listview_tabela);
mylistview.setAdapter(myCursorAdapter);
}
}

和DBadapter代码:

public class DBadapter {
private static final String TAG = "DBadapter";

public static final String KEY_ROWID = "_id";
public static final int COL_ROWID = 0;
public static final String KEY_NAME = "name";
public static final String KEY_GAMES = "games_no";
public static final String KEY_WON = "won";
public static final String KEY_LOST= "lost";
public static final String KEY_DIFF="difference";
public static final String KEY_POINTS="points";

public static final int COL_NAME = 1;
public static final int COL_GAMES = 2;
public static final int COL_WON = 3;
public static final int COL_LOST= 4;
public static final int COL_DIFF= 5;
public static final int COL_POINTS= 6;

public static final String[] ALL_KEYS = new String[] {KEY_ROWID, KEY_NAME, KEY_GAMES, KEY_WON, KEY_LOST, KEY_DIFF, KEY_POINTS};

// DB info: it's name, and the table we are using (just one).
public static final String DATABASE_NAME = "mydaatabase";
public static final String DATABASE_TABLE = "baske_tbl";
// Track DB version if a new version of your app changes the format.
public static final int DATABASE_VERSION = 1;   

private static final String DATABASE_CREATE_SQL = 
        "create table " + DATABASE_TABLE 
        + " (" + KEY_ROWID + " integer primary key autoincrement, "
        + KEY_NAME + " text not null, "
        + KEY_GAMES + " integer not null, "
        + KEY_WON + " integer not null"
        + KEY_LOST + " integer not null, "
        + KEY_DIFF + " integer not null, "
        + KEY_POINTS + " integer not null "

        + ");";

// Context of application who uses us.
private final Context context;

private DatabaseHelper myDBHelper;
private SQLiteDatabase db;


public DBadapter(Context ctx) {
    this.context = ctx;
    myDBHelper = new DatabaseHelper(context);
}

// Open the database connection.
public DBadapter open() {
    db = myDBHelper.getWritableDatabase();
    return this;
}

// Close the database connection.
public void close() {
    myDBHelper.close();
}
// Return all data in the database.
public Cursor getAllRows() {
    String where = null;
    Cursor c =  db.query(true, DATABASE_TABLE, ALL_KEYS, 
                        where, null, null, null, null, null);
    if (c != null) {
        c.moveToFirst();
    }
    return c;
}

private static class DatabaseHelper extends SQLiteOpenHelper
{
    DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    @Override
    public void onCreate(SQLiteDatabase database) {
        database.execSQL(DATABASE_CREATE_SQL);      
    }

    @Override
    public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
        Log.w(TAG, "Upgrading application's database from version " + oldVersion
                + " to " + newVersion + ", which will destroy all old data!");

        // Destroy old database:
        database.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);

        // Recreate new database:
        onCreate(database);
    }

        }
}

还有两个布局,一个是名为item_layout.xml的行布局,另一个是list_activity.xml,其中包含listView。 每次我尝试运行它都会显示“进程已意外停止”。任何解决方案?

0 个答案:

没有答案