SQLite应用程序根本不会运行。出现强制关闭消息

时间:2015-06-11 18:50:42

标签: android-sqlite

我正在使用SQLite创建一个应用程序 它在装载前崩溃 我修复了所有可行的错误,因为我甚至没有得到日志信息,所以我很难搞清楚错误。

请帮忙。

提供SQLHelper

public class MySQLiteHelper extends SQLiteOpenHelper {

// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "MediaDB";
// Media table name
private static final String TABLE_MEDIA = "media";

// Media Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_TYPE = "type";
private static final String KEY_TITLE = "title";
private static final String KEY_AUTHOR = "author";

private static final String[] COLUMNS = {KEY_ID,KEY_TYPE,KEY_TITLE,KEY_AUTHOR};

public MySQLiteHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);  
}

@Override
public void onCreate(SQLiteDatabase db) {
    // SQL statement to create media table
    String CREATE_MEDIA_TABLE = "CREATE TABLE media ( " +
            "id INTEGER PRIMARY KEY AUTOINCREMENT, " + 
            "tyoe TYPE, "+
            "title TEXT, "+
            "author TEXT )";

    // create media table
    db.execSQL(CREATE_MEDIA_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Drop older media table if existed
    db.execSQL("DROP TABLE IF EXISTS media");

    // create fresh media table
    this.onCreate(db);
}

//ADD MEDIA
public void addMedia(Media media){
    //for logging
    Log.d("addMedia", media.toString()); 

    // get reference to writable DB
    SQLiteDatabase db = this.getWritableDatabase();

    // create ContentValues to add key "column"/value
    ContentValues values = new ContentValues();
    values.put(KEY_TYPE, media.getType()); // get title 
    values.put(KEY_TITLE, media.getTitle()); // get title 
    values.put(KEY_AUTHOR, media.getAuthor()); // get author

    // insert
    db.insert(TABLE_MEDIA, // table
    null, //nullColumnHack
    values); // key/value -> keys = column names/ values = column values

    // close
    db.close(); 
}

//GET MEDIA
public Media getMedia(int id){

    // get reference to readable DB
    SQLiteDatabase db = this.getReadableDatabase();

    // build query
    Cursor cursor = 
            db.query(TABLE_MEDIA, // table
            COLUMNS, // column names
            " id = ?", // selections 
            new String[] { String.valueOf(id) }, // d. selections args
            null, // group by
            null, // having
            null, // order by
            null); // limit

    // if we got results get the first one
    if (cursor != null)
        cursor.moveToFirst();

    // build media object
    Media media = new Media();
    media.setId(Integer.parseInt(cursor.getString(0)));
    media.setType(cursor.getString(1));
    media.setTitle(cursor.getString(2));
    media.setAuthor(cursor.getString(3));

    //log 
Log.d("getMedia("+id+")", media.toString());

    // return media
    return media;
}

//GET ALL MEDIA
public List<Media> getAllMedia() {
    List<Media> medias = new LinkedList<Media>();

    // build the query
    String query = "SELECT  * FROM " + TABLE_MEDIA;

    // get reference to writable DB
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(query, null);

    // go over each row, build media and add it to list
    Media media = null;
    if (cursor.moveToFirst()) {
        do {
            media = new Media();
            media.setId(Integer.parseInt(cursor.getString(0)));
            media.setType(cursor.getString(1));
            media.setTitle(cursor.getString(2));
            media.setAuthor(cursor.getString(3));

            // Add media to media
            medias.add(media);
        } while (cursor.moveToNext());
    }

    Log.d("getAllMedia()", medias.toString());

    // return media
    return medias;
}

//UPDATE
public int updateMedia(Media media) {

    // get reference to writable DB
    SQLiteDatabase db = this.getWritableDatabase();

    // create ContentValues to add key "column"/value
    ContentValues values = new ContentValues();
    values.put("type", media.getType()); // get title 
    values.put("title", media.getTitle()); // get title 
    values.put("author", media.getAuthor()); // get author

    // updating row
    int i = db.update(TABLE_MEDIA, //table
            values, // column/value
            KEY_ID+" = ?", // selections
            new String[] { String.valueOf(media.getId()) }); //selection args

    // close
    db.close();

    return i;

}

//DELETE
public void deleteMedia(Media media) {

    // get reference to writable DB
    SQLiteDatabase db = this.getWritableDatabase();

    // delete
    db.delete(TABLE_MEDIA, //table name
            KEY_ID+" = ?",  // selections
            new String[] { String.valueOf(media.getId()) }); //selections args

    // close
    db.close();

    //log
Log.d("deleteMedia", media.toString());

}
}

媒体对象类

public class Media {

private int id;
private String type;
private String title;
private String author;

public Media(){}

public Media(String type, String title, String author) {
    super(); 
    this.type = type;
    this.title = title;
    this.author = author;
}

//getters & setters
// getting ID
public int getId(){
    return this.id;
}

// setting id
public void setId(int id){
    this.id = id;
}
// getting type
public String getType(){
    return this.type;
}

// setting title
public void setType(String type){
    this.type = type;
}
// getting title
public String getTitle(){
    return this.title;
}

// setting title
public void setTitle(String title){
    this.title = title;
}
// getting author
public String getAuthor(){
    return this.author;
}

// setting author
public void setAuthor(String author){
    this.author = author;
}

@Override
public String toString() {
    return "Media [id=" + id + ", type=" + type + ",title=" + title + ", author=" + author
            + "]";
}
}

主要活动

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

MySQLiteHelper db = new MySQLiteHelper(this);


/**
 * CRUD Operations
 * */
// add Media
db.addMedia(new Media("Book", "Android Application Development Cookbook", "Wei Meng Lee"));   
db.addMedia(new Media("Book", "Android Programming: The Big Nerd Ranch Guide", "Bill Phillips and Brian Hardy"));       
db.addMedia(new Media("Book", "Learn Android App Development", "Wallace Jackson"));

// get all media
List<Media> list = db.getAllMedia();

// delete one media
db.deleteMedia(list.get(0));

// get all media
db.getAllMedia();

}
}

1 个答案:

答案 0 :(得分:0)

您创建表格时出现错误:

String CREATE_MEDIA_TABLE = "CREATE TABLE media ( " +
        "id INTEGER PRIMARY KEY AUTOINCREMENT, " + 
        "tyoe TYPE, "+
        "title TEXT, "+
        "author TEXT )";

TYPE 不是有效的SQLlite数据类型 请参阅此页面:https://www.sqlite.org/datatype3.html

我将您的表格创建为

String CREATE_MEDIA_TABLE = "CREATE TABLE media (" +
        "id INTEGER PRIMARY KEY AUTOINCREMENT, " + 
        "tyoe TEXT, "+
        "title TEXT, "+
        "author TEXT)";