我正在尝试将数据插入到SQLite数据库中,但是应用程序因为某些原因而崩溃,我对此一无所知。我刚刚学会了“如何创建数据库和表,如何插入数据(但似乎没有)”请不要问我Android Studio对崩溃的说法是什么我无法在笔记本电脑上运行app “Android模拟器或其他任何东西”的帮助。
以下是我的DatabaseHelper代码,扩展SQLiteOpenHelper
package com.famiddle.genius.businessmanager;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {
// DECLARING NAME FOR THE DATABASE AND THE TABLE
public static final String DATABASE_NAME = "imported_items";
public static final String TABLE_NAME = "stock";
// DECLARING NAME FOR THE COLUMNS
public static final String COL_1 = "ID";
public static final String COL_2 = "ITEMS_NAME";
public static final String COL_3 = "QUANTITY";
public static final String COL_4 = "COST";
public static final String COL_5 = "TOTAL_COST";
// DEFAULT CONSTRUCTOR
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
// IT CREATES TABLE
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, ITEMS_NAME TEXT, QUANTITY INTEGER, COST INTEGER, TOTAL_COST INTEGER");
}
// IT DROPS TABLE, IF IT ALREADY EXISTS
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db); //I DON'T KNOW WHY IT IS HERE, PLEASE TELL ME MORE ABOUT
// IT IN COMMENTS
}
// IT INSERTS DATA OR VALUES IN TABLE
public boolean insertData(String itemsName, int quantity, int cost, int totalCost) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2, itemsName);
contentValues.put(COL_3, quantity);
contentValues.put(COL_4, cost);
contentValues.put(COL_5, totalCost);
long result = db.insert(TABLE_NAME, null, contentValues);
//db.close();
if (result == -1){
return false;
}else {return true;}
// return result != -1;
}
}
这是库存片段的代码,其中正在调用数据库实例 和使用
public class Stock extends Fragment {
DatabaseHelper dataBase;
ArrayList<String> items;
SharedPreferences prefs;
SharedPreferences.Editor editor;
public Stock() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.stock, container, false);
prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
editor = prefs.edit();
if(prefs.getBoolean("has_not_created?", true)) {
// INITIALIZING ARRAY LIST THAT CONTAINS ITEMS NAME
items = new ArrayList<>();
items.add("One");
items.add("Two");
items.add("Three");
items.add("Four");
items.add("Five");
items.add("Six");
// INITIALIZING DATABASE
dataBase = new DatabaseHelper(getContext());
// CREATING A LOOP FOR INSERTING ITEMS NAMES ONE BY ONE
for (int i = 0; i < items.size(); i++) {
Boolean isInserted = dataBase.insertData(items.get(i), 0, 0, 0);
Toast.makeText(getActivity(), items.get(i) + " has been inserted into database" + isInserted , Toast.LENGTH_SHORT).show();
}
// PUTTING 'FALSE' IN SHARED PREFERENCE OBJECT
// THEN IT WILL NOT ALLOW THE CODE TO RUN
editor.putBoolean("has_not_created?", false);
editor.apply();
}
有人可以告诉我我的代码有什么问题吗?我是从Android教程中学到的。
答案 0 :(得分:0)
您的错误发生在onCreate()
。
您要打开括号,但不要在任何地方关闭它。 所以它应该是::
db.execSQL("CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, ITEMS_NAME TEXT, QUANTITY INTEGER, COST INTEGER, TOTAL_COST INTEGER)");