我是android studio的初学者,所以我在构建我的第一个应用程序时磕磕绊绊。尝试将数据保存到数据库时,我想出了错误。每当我尝试保存任何数字时,应用程序都会崩溃。
数据库Java文件:
package com.miahollins.basketballfinal;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by pumpkin on 6/17/16.
*/
public class DataHandler {
public static final String GAMES = "games_played";
public static final String GAMES2 = "games_started";
public static final String GAMES3 = "points";
public static final String TABLE_NAME = "statstable";
public static final String DATA_BASE_NAME = "mydatabase.sqlite";
public static final int DATABASE_VERSION = 2;
public static final String TABLE_CREATE = "create table statstable (games_played VARCHAR not null, games_started VARCHAR not null, points VARCHAR not null)";
DatabaseHelper dbhelper;
Context ctx;
SQLiteDatabase db;
public DataHandler(Context ctx){
this.ctx = ctx;
dbhelper = new DatabaseHelper(ctx);
}
private static class DatabaseHelper extends SQLiteOpenHelper{
public DatabaseHelper(Context ctx){
super(ctx, DATA_BASE_NAME,null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(TABLE_CREATE);
} catch(SQLException e){
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS statstable");
onCreate(db);
}
}
public DataHandler open(){
db = dbhelper.getWritableDatabase();
return this;
}
public void close(){
dbhelper.close();
}
public long insertData(String games_played, String games_started, String points){
ContentValues content = new ContentValues();
content.put(GAMES, games_played);
content.put(GAMES2, games_started);
content.put(GAMES3, points);
return db.insertOrThrow(TABLE_NAME, null, content);
}
public Cursor returnData(){
return db.query(TABLE_NAME, new String[]{GAMES, GAMES2, GAMES3},null, null, null, null, null);
}
}
在我的logcat中,我收到错误消息,指出无法找到列。任何帮助表示赞赏!