I am having trouble inserting records to my application local database. I am getting data from web services and tried to store them inside my app local sqlite database but i am getting no such table error. This is the code of my DatabaseHelper class that i am using for table creation & inserting into it.
我在将记录插入应用程序本地数据库时遇到问题。我从Web服务获取数据并尝试将它们存储在我的应用程序本地sqlite数据库中,但我没有得到这样的表错误。这是我用于表创建和插入的DatabaseHelper类的代码。
private static final String CREATE_MERCHANT_DETAILS = "CREATE TABLE " + TABLE_MERCHANT_DETAILS +
" (" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_NAME + " TEXT, "
+ KEY_LOGO + "TEXT, "
+ KEY_BACKGROUND + "TEXT, "
+ KEY_PRODUCT + "TEXT, "
+ KEY_PRODUCT_ID + "TEXT, "
+ KEY_TYPE + "TEXT, "
+ KEY_DENOMINATION + "TEXT);";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_MERCHANT_DETAILS);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void insertAMerchant(String name, String logo, String background, String product, String productId, String type, String denomination) {
database = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(KEY_NAME, name);
contentValues.put(KEY_LOGO, logo);
contentValues.put(KEY_BACKGROUND, background);
contentValues.put(KEY_PRODUCT, product);
contentValues.put(KEY_PRODUCT_ID, productId);
contentValues.put(KEY_TYPE, type);
contentValues.put(KEY_DENOMINATION, denomination);
database.insert(TABLE_MERCHANT_DETAILS, null, contentValues);
}
2 个解决方案
#1
Add spaces between your column names and types in your CREATE TABLE
SQL.
在CREATE TABLE SQL中的列名和类型之间添加空格。
Uninstall your app so that the old database file gets removed and your onCreate()
is run again.
卸载您的应用程序,以便删除旧的数据库文件并再次运行onCreate()。
#2
Your question title says,
你的问题标题说,
No such column exists error when inserting into sqlite database
插入sqlite数据库时,没有这样的列存在错误
and in your question you're saying,
在你的问题中,你说,
no such table error.
没有这样的表错误。
If the error is No such column exists
, the problem can be because of
如果错误为“否”,则存在此列,问题可能是因为
- Spacing between the column name and Type
-
If you have changed your Table Schema then you need to increment your
DATABASE_VERSION
to reflect the change. But, you need to implement and setuponUpgrade
to do so. I've added code here.
列名称和类型之间的间距
如果您更改了表模式,则需要增加DATABASE_VERSION以反映更改。但是,您需要实现并设置onUpgrade才能这样做。我在这里添加了代码。
You need to setup onUpgrade
to reflect the new changes. You can do by,
您需要设置onUpgrade以反映新的更改。你可以做,
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i2) {
Toast.makeText(context, "on Upgrade is called", Toast.LENGTH_LONG).show();
db.execSQL("DROP TABLE IF EXISTS " + TABLE_MERCHANT_DETAILS);
onCreate(db);
}
To check whether your Sql query is valid/not, print the query in the DatabaseHelper.
要检查您的Sql查询是否有效,请在DatabaseHelper中打印查询。
System.out.println(CREATE_MERCHANT_DETAILS);
This will display the query and you can check exactly where you made the mistake.
这将显示查询,您可以确切地检查错误的位置。
#1
Add spaces between your column names and types in your CREATE TABLE
SQL.
在CREATE TABLE SQL中的列名和类型之间添加空格。
Uninstall your app so that the old database file gets removed and your onCreate()
is run again.
卸载您的应用程序,以便删除旧的数据库文件并再次运行onCreate()。
#2
Your question title says,
你的问题标题说,
No such column exists error when inserting into sqlite database
插入sqlite数据库时,没有这样的列存在错误
and in your question you're saying,
在你的问题中,你说,
no such table error.
没有这样的表错误。
If the error is No such column exists
, the problem can be because of
如果错误为“否”,则存在此列,问题可能是因为
- Spacing between the column name and Type
-
If you have changed your Table Schema then you need to increment your
DATABASE_VERSION
to reflect the change. But, you need to implement and setuponUpgrade
to do so. I've added code here.
列名称和类型之间的间距
如果您更改了表模式,则需要增加DATABASE_VERSION以反映更改。但是,您需要实现并设置onUpgrade才能这样做。我在这里添加了代码。
You need to setup onUpgrade
to reflect the new changes. You can do by,
您需要设置onUpgrade以反映新的更改。你可以做,
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i2) {
Toast.makeText(context, "on Upgrade is called", Toast.LENGTH_LONG).show();
db.execSQL("DROP TABLE IF EXISTS " + TABLE_MERCHANT_DETAILS);
onCreate(db);
}
To check whether your Sql query is valid/not, print the query in the DatabaseHelper.
要检查您的Sql查询是否有效,请在DatabaseHelper中打印查询。
System.out.println(CREATE_MERCHANT_DETAILS);
This will display the query and you can check exactly where you made the mistake.
这将显示查询,您可以确切地检查错误的位置。