我是Android Studio和数据库的初学者。但是,在视频和指南之后,我能够设置一个基本的。
这样做后,我想扩展它以创建2个额外的表。但是后来当我试图将数据输入到后面的表时,应用程序崩溃了,我似乎无法找到它的错误。我尝试了不同的格式化语法,但它在同一事件(当我尝试将数据提交到表)时不断崩溃。
//Following is the DatabaseHelper class that set up the database.
public class DatabaseHelper extends SQLiteOpenHelper{
//contacts
private static final String TABLE_CONTACTS = "contacts";
private static final String COLUMN_ID = "id";
private static final String COLUMN_NAME = "name";
private static final String COLUMN_EMAIL = "email";
private static final String COLUMN_PHONENUMBER = "phonenumber";
private static final String COLUMN_UNAME = "uname";
private static final String COLUMN_PASS = "pass";
//inventory
private static final String TABLE_ORGANIZATIONS = "organizations";
private static final String COLUMN_ORGANIZATION_ITEMID = "itemid";
private static final String COLUMN_ORGANIZATION_ITEMNAME = "itemname";
private static final String COLUMN_ORGANIZATION_PRICE = "price";
private static final String COLUMN_ORGANIZATION_QUANTITY = "quantity";
private static final String COLUMN_ORGANIZATION_DESCRIPTION = "description";
//management
private static final String TABLE_MANAGEMENTS = "managements";
private static final String COLUMN_MANAGEMENT_EVENTID = "eventid";
private static final String COLUMN_MANAGEMENT_EVENTNAME = "eventname";
private static final String COLUMN_MANAGEMENT_EVENTDATE = "eventdate";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "inventstory.db";
//decare database variable
SQLiteDatabase db;
//create a table for to hold values.
private static final String TABLE_CREATE_CONTACTS = "create table " + TABLE_CONTACTS + "("
+ COLUMN_ID + " integer primary key, "
+ COLUMN_NAME + " text not null , "
+ COLUMN_EMAIL + " text not null , "
+ COLUMN_PHONENUMBER + " text not null , "
+ COLUMN_UNAME + " text not null , "
+ COLUMN_PASS + " text not null" + ");";
//create a table for to hold values.
private static final String TABLE_CREATE_ORGANIZATIONS = "create table " + TABLE_ORGANIZATIONS + "("
+ COLUMN_ORGANIZATION_ITEMID + " integer primary key autoincrement, "
+ COLUMN_ORGANIZATION_ITEMNAME + " text not null , "
+ COLUMN_ORGANIZATION_PRICE + " text not null , "
+ COLUMN_ORGANIZATION_QUANTITY + " text not null , "
+ COLUMN_ORGANIZATION_DESCRIPTION + " text not null " + ");";
//create a table for to hold values.
private static final String TABLE_CREATE_MANAGEMENTS = " create table " + TABLE_MANAGEMENTS + "("
+ COLUMN_MANAGEMENT_EVENTID + " integer primary key, "
+ COLUMN_MANAGEMENT_EVENTNAME + " text not null , "
+ COLUMN_MANAGEMENT_EVENTDATE + " text not null " + ");";
//constructor
public DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void insertOrganization(inventoryorg c){
//to insert to the database, use 'getWrite...' to make connection
db = this.getWritableDatabase();
//create content values
ContentValues values = new ContentValues();
// '*' means everything
// fetch the data
String query = " select * from 'organizations' ";
Cursor cursor = db.rawQuery(query, null);
int count = cursor.getCount(); //what does this do
// values.put(COLUMN_ORGANIZATION_ITEMID, count); // should this be changed to 'c.getItemid()'
values.put(COLUMN_ORGANIZATION_ITEMNAME, c.getItemname());
values.put(COLUMN_ORGANIZATION_PRICE, c.getPrice());
values.put(COLUMN_ORGANIZATION_QUANTITY, c.getQuantity());
values.put(COLUMN_ORGANIZATION_DESCRIPTION, c.getDescription());
db.insert(TABLE_ORGANIZATIONS, null, values);
}
@Override
public void onCreate(SQLiteDatabase db) {
//change
//db.execSQL(TABLE_CREATE_CONTACTS);
//db.execSQL(TABLE_CREATE_ORGANIZATIONS);
//db.execSQL(TABLE_CREATE_MANAGEMENTS);
db.execSQL(TABLE_CREATE_CONTACTS+TABLE_CREATE_ORGANIZATIONS+TABLE_CREATE_MANAGEMENTS);
this.db = db;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);
// db.execSQL("DROP TABLE IF EXISTS " + TABLE_ORGANIZATIONS);
//db.execSQL("DROP TABLE IF EXISTS " + TABLE_MANAGEMENTS);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS
+ " DROP TABLE IF EXISTS " + TABLE_ORGANIZATIONS
+ " DROP TABLE IF EXISTS " + TABLE_MANAGEMENTS); //same result
this.onCreate(db);
}
以下代码是将数据插入表中的函数(来自OrgInsert.java)(在Databasehelper.java上为decalred)。
public void onClickOrgSubmitButton(View v){
if(v.getId()==R.id.BSubmitButton){
//we read data from signup
EditText itemname = (EditText) findViewById(R.id.TFitemname);
EditText price = (EditText) findViewById(R.id.TFprice);
EditText quantity = (EditText) findViewById(R.id.TFquantity);
EditText description = (EditText) findViewById(R.id.TFdescription);
//convert them to string values
String itemnamestr = itemname.getText().toString();
String pricestr = price.getText().toString();
String quantitystr = quantity.getText().toString();
String descriptionstr = description.getText().toString();
//insert to the database
inventoryorg inv = new inventoryorg();
inv.setItemname(itemnamestr);
inv.setPrice(pricestr);
inv.setQuantity(quantitystr);
inv.setDescription(descriptionstr);
//this one needs to be enforced.
helper.insertOrganization(inv);
//popup message.
Toast pass = Toast.makeText(OrgInsert.this, "Successfully created.", Toast.LENGTH_LONG);
pass.show();
//'starting new activity.' To start, we need to make object of Intent class
//This takes back to the 'Organization' after new data is inserted.
Intent i = new Intent(OrgInsert.this, Organization.class);
startActivity(i);
}
}
引起:android.database.sqlite.SQLiteException:没有这样的表:组织(代码1):,同时编译:select * from' organization'
- 醇>
表尚未创建 怀疑:'计数'来自value.put的ItemID
// java.lang.IllegalStateException:无法执行android的方法:onClick
//
// at com.example.edward.inventstoryreformat.OrgInsert.onSubmitButton(OrgInsert.java:51)
我认为:51是helper.insertOrganization(inv)
。
以上信息是我尝试解决的一些错误消息。 这让我很困惑,因为联系人'创建没有任何问题,这是由' SignUp.java'完成的。我使用SQLite Manager检查了数据库,并验证了这些是否已创建和保存。
答案 0 :(得分:0)
在 insertOrganization()方法中,在SELECT语句中将 ' 更改为组织。
更新(2016/04/20):
正如所承诺的,有关SQL语句中引号的一些信息:
[S]单引号用于SQL查询中的[S]文字。由于组织应该已经创建,因此它已经是数据库的表对象。因此,在这种情况下不使用单引号。
[D]双引号用于[D] atabase中的保留关键字。因此,如果您的表名为插入,则应使用 SELECT * FROM" insert" 。您可以尝试双引号,但它应该给出相同的错误。尽管如此,在引用数据库中的现有对象时,请不要使用单引号。
此外,在再次检查您的代码和更新后,我发现了一些您应该仔细检查的事项:
您的TABLE_CREATE_ORGANIZATIONS指出您的COLUMN_ORGANIZATION_ITEMID应自动递增。这意味着您不必为该表显式设置id列。因此,您在INSERT代码中根本不需要以下行,这实际上是当前的错误导致点:
String query = " select * from 'organizations' ";
Cursor cursor = db.rawQuery(query, null);
int count = cursor.getCount(); //what does this do
此外,请不要忘记在打开数据库后使用db.close()关闭数据库。在这种情况下,它应该在insertOrganization()的最后几行,onCreate()和onUpdate()方法
最后,在所有TABLE_CREATE_XXX字符串常量中,使用CREATE TABLE IF NOT EXISTS(...),以确保您没有覆盖任何内容。
如果有任何问题可以帮助我。如果它没有,我将为您提供一种扩展SQLiteOpenHelper类的不同方法。
答案 1 :(得分:0)
试试这个!!
public void insertOrganization(inventoryorg c){
//to insert to the database, use 'getWrite...' to make connection
db = this.getWritableDatabase();
//create content values
ContentValues values = new ContentValues();
// values.put(COLUMN_ORGANIZATION_ITEMID, count); // should this be changed to 'c.getItemid()'
values.put(COLUMN_ORGANIZATION_ITEMNAME, c.getItemname());
values.put(COLUMN_ORGANIZATION_PRICE, c.getPrice());
values.put(COLUMN_ORGANIZATION_QUANTITY, c.getQuantity());
values.put(COLUMN_ORGANIZATION_DESCRIPTION, c.getDescription());
db.insert(TABLE_ORGANIZATIONS, null, values);
db.close();
}