我试图按照此链接中的说明操作:http://www.androiddesignpatterns.com/2012/05/correctly-managing-your-sqlite-database.html 在方法#1下从不同的活动访问我的数据库。但是没有插入数据,我收到了一些错误。
我用来插入数据的主要类:
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class newCharacter extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_character);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Button submit_button = (Button) findViewById(R.id.char_submit);
DatabaseHelper.getInstance(getApplicationContext());
EditText name_text = (EditText) findViewById(R.id.nameText);
EditText health_text = (EditText) findViewById(R.id.editText);
EditText strength_text = (EditText) findViewById(R.id.editText2);
EditText agility_text = (EditText) findViewById(R.id.editText3);
EditText intelligence_text = (EditText) findViewById(R.id.editText4);
EditText charisma_text = (EditText) findViewById(R.id.editText5);
EditText endurance_text = (EditText) findViewById(R.id.editText6);
EditText carry_weight_text = (EditText) findViewById(R.id.editText7);
EditText zprep_text = (EditText) findViewById(R.id.editText8);
EditText inventory_text = (EditText) findViewById(R.id.editText9);
final String name = name_text.toString();
final String health = health_text.toString();
final String strength = strength_text.toString();
final String agility = agility_text.toString();
final String intelligence = intelligence_text.toString();
final String charisma = charisma_text.toString();
final String endurance = endurance_text.toString();
final String carryweight = carry_weight_text.toString();
final String zprep = zprep_text.toString();
final String inventory = inventory_text.toString();
submit_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ERROR HERE boolean isInserted = DatabaseHelper.getInstance(getApplicationContext()).insertData(name, health, strength,
endurance, carryweight, agility, intelligence, charisma, zprep, inventory);
if (isInserted == true)
Toast.makeText(newCharacter.this, "Data Inserted", Toast.LENGTH_LONG).show();
else
Toast.makeText(newCharacter.this, "Data NOT Inserted", Toast.LENGTH_LONG).show();
}
});
}
}
我的DatabaseHelper:
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by Matt on 11/23/2015.
*/
public class DatabaseHelper extends SQLiteOpenHelper {
private static DatabaseHelper sInstance;
public static final String DATABASE_NAME = "character_info.db";
public static final String TABLE_NAME = "characters_table";
public static final String COL_1 = "NAME";
public static final String COL_3 = "HEALTH";
public static final String COL_2 = "STRENGTH";
public static final String COL_4 = "ENDURANCE";
public static final String COL_5 = "CARRY_WEIGHT";
public static final String COL_6 = "AGILITY";
public static final String COL_7 = "INTELLIGENCE";
public static final String COL_8 = "CHARISMA";
public static final String COL_9 = "ZPREP";
public static final String COL_10 = "INVENTORY";
public static synchronized DatabaseHelper getInstance(Context context) {
if (sInstance == null) {
sInstance = new DatabaseHelper(context.getApplicationContext());
}
return sInstance;
}
private DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
String SQL_String = "create table " + TABLE_NAME +" (NAME TEXT PRIMARY KEY, HEALTH TEXT, STRENGTH TEXT" +
", ENDURANCE TEXT, CARRY_WEIGHT TEXT, AGILITY TEXT, INTELLIGENCE TEXT, CHARISMA TEXT, ZPREP TEXT, INVENTORY TEXT)";
db.execSQL(SQL_String);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
onCreate(db);
}
public boolean insertData(String name, String health, String strength, String endurance, String carryweight,
String agility, String intelligence, String charisma, String zprep, String inventory) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_1, name);
contentValues.put(COL_2, health);
contentValues.put(COL_3, strength);
contentValues.put(COL_4, endurance);
contentValues.put(COL_5, carryweight);
contentValues.put(COL_6, agility);
contentValues.put(COL_7, intelligence);
contentValues.put(COL_8, charisma);
contentValues.put(COL_9, zprep);
contentValues.put(COL_10, inventory);
ERROR HERE: long result = db.insert(TABLE_NAME, null, contentValues);
if (result == -1)
return false;
else
return true;
}
}
logcat:
12-01 01:05:45.049 274-274/proctor.csit.project E/Database: Error inserting INVENTORY=android.support.v7.widget.AppCompatEditText@45f78508 INTELLIGENCE=android.support.v7.widget.AppCompatEditText@45f9e0f0 CHARISMA=android.support.v7.widget.AppCompatEditText@45f7ab58 NAME=android.support.v7.widget.AppCompatEditText@45f663f0 ZPREP=android.support.v7.widget.AppCompatEditText@45f7ed60 HEALTH=android.support.v7.widget.AppCompatEditText@45f8dfd0 CARRY_WEIGHT=android.support.v7.widget.AppCompatEditText@45f634f0 ENDURANCE=android.support.v7.widget.AppCompatEditText@45f8ef60 STRENGTH=android.support.v7.widget.AppCompatEditText@45f7dda8 AGILITY=android.support.v7.widget.AppCompatEditText@45f9db10
12-01 01:05:45.049 274-274/proctor.csit.project E/Database: android.database.sqlite.SQLiteException: table characters_table has no column named INVENTORY: , while compiling: INSERT INTO characters_table(INVENTORY, INTELLIGENCE, CHARISMA, NAME, ZPREP, HEALTH, CARRY_WEIGHT, ENDURANCE, STRENGTH, AGILITY) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?);
12-01 01:05:45.049 274-274/proctor.csit.project E/Database: at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
12-01 01:05:45.049 274-274/proctor.csit.project E/Database: at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91)
12-01 01:05:45.049 274-274/proctor.csit.project E/Database: at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64)
12-01 01:05:45.049 274-274/proctor.csit.project E/Database: at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:80)
12-01 01:05:45.049 274-274/proctor.csit.project E/Database: at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:36)
12-01 01:05:45.049 274-274/proctor.csit.project E/Database: at android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1145)
12-01 01:05:45.049 274-274/proctor.csit.project E/Database: at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1536)
12-01 01:05:45.049 274-274/proctor.csit.project E/Database: at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1410)
12-01 01:05:45.049 274-274/proctor.csit.project E/Database: at proctor.csit.project.DatabaseHelper.insertData(DatabaseHelper.java:70)
12-01 01:05:45.049 274-274/proctor.csit.project E/Database: at proctor.csit.project.newCharacter$1.onClick(newCharacter.java:55)
12-01 01:05:45.049 274-274/proctor.csit.project E/Database: at android.view.View.performClick(View.java:2408)
12-01 01:05:45.049 274-274/proctor.csit.project E/Database: at android.view.View$PerformClick.run(View.java:8816)
12-01 01:05:45.049 274-274/proctor.csit.project E/Database: at android.os.Handler.handleCallback(Handler.java:587)
12-01 01:05:45.049 274-274/proctor.csit.project E/Database: at android.os.Handler.dispatchMessage(Handler.java:92)
12-01 01:05:45.049 274-274/proctor.csit.project E/Database: at android.os.Looper.loop(Looper.java:123)
12-01 01:05:45.049 274-274/proctor.csit.project E/Database: at android.app.ActivityThread.main(ActivityThread.java:4627)
12-01 01:05:45.049 274-274/proctor.csit.project E/Database: at java.lang.reflect.Method.invokeNative(Native Method)
12-01 01:05:45.049 274-274/proctor.csit.project E/Database: at java.lang.reflect.Method.invoke(Method.java:521)
12-01 01:05:45.049 274-274/proctor.csit.project E/Database: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-01 01:05:45.049 274-274/proctor.csit.project E/Database: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-01 01:05:45.049 274-274/proctor.csit.project E/Database: at dalvik.system.NativeStart.main(Native Method)
我在我的java代码中的明显错误旁边添加了注释。我第一次使用sqlite而且我很遗憾,所以感谢任何帮助!
答案 0 :(得分:4)
更改数据库架构时,需要增加版本号并在onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
中实施升级。在你的情况下,它可能看起来像这样:
private static final int VERSION = 2; // increased from 1
private DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, VERSION);
}
...
@Override
public void onUpdgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
switch (oldVersion) {
case 1:
db.execSQL("ALTER TABLE " + TABLE_NAME + " ADD COLUMN INVENTORY TEXT");
// intentionally fall through to other cases
}
}
这支持用户下载应用更新且其数据库的模式已过时的情况。
如果您的应用尚未发布,您可以轻松更改架构,无需执行上面所写的操作,然后只需清除应用数据(或卸载和重新安装,具有相同的效果)。这将导致您的数据库重新创建,因此它将再次执行CREATE sql。
答案 1 :(得分:1)
如果您最近添加inventory
列,请从您使用的手机或模拟器中卸载应用并尝试新的!
答案 2 :(得分:1)
转到设置,然后转到应用程序,然后转到所有选项卡,然后选择您的应用程序,然后单击清除数据,然后尝试再次运行应用程序它应该工作。
发生此错误是因为在添加库存表后未调用sqliteopenhelper类的onCreate。当已存在具有该名称的数据库时,不会调用Sqliteopenheler的onCreate。 onUpgrade也没有被调用,因为你没有增加sqliteopenhelper中的版本号,因此你的表库存没有被创建,因此你得到了错误。
通常,如果您的应用程序仍在开发中,并且您是唯一正在开发它的开发人员,那么您可以做的只是清除应用程序的数据,这将删除所有数据(包括您的数据库),因此当您运行时应用程序将再次调用onCreate。但是,如果您的应用程序正在生产中,则需要增加应用程序的版本号并将升级逻辑的代码放在onUpgrade中