Android Studio-SQLite插入冻结



我正在尝试制作一个SQLite应用程序,在那里我可以添加和删除行,并且应该立即打印出来
当我按下其中一个按钮时,模拟器会冻结
我没有错误,logcat没有显示任何内容
我在模拟器和实际设备上进行了尝试,结果相同

这是我没有导入的主要活动:

public class MainActivity extends AppCompatActivity {
//in the tutorial these variables were not private
private EditText et1;
private TextView tv1;
private MyDBHandler dbHandler;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1 = (EditText) findViewById(R.id.editText);
tv1 = (TextView) findViewById(R.id.textView);
dbHandler = new MyDBHandler(this, null, null, 1);
}
public void insertButtonClicked(View view) {
Products product = new Products(et1.getText().toString());
dbHandler.addProduct(product);
printDatabase();
}
public void deleteButtonClicked(View view) {
String inputText = et1.getText().toString();
dbHandler.deleteProduct(inputText);
printDatabase();
}
public void printDatabase() {
String dbString = dbHandler.databaseToString();
tv1.setText(dbString);
et1.setText("");
}
}

类别产品:

public class Products {
private int _id;
private String _productname;
public Products(String productname) {
this._productname = productname;
}
public int get_id() {
return _id;
}
public String get_productname() {
return _productname;
}
public void set_id(int _id) {
this._id = _id;
}
public void set_productname(String productname) {
this._productname = productname;
}
}

没有导入的类myDBHandler:


public class MyDBHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "products.db";
public static final String TABLE_PRODUCTS = "products";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_PRODUCTNAME = "_productname";
public MyDBHandler(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String query = "CREATE TABLE " + TABLE_PRODUCTS + " (" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_PRODUCTNAME + " TEXT" +
");";
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_PRODUCTS);
onCreate(db);
}
//add a new row to the database
public void addProduct(Products product) {
ContentValues values = new ContentValues();
values.put(COLUMN_PRODUCTNAME, product.get_productname());
SQLiteDatabase db = getWritableDatabase();
db.insert(TABLE_PRODUCTS, null, values);
}
//delete a row from the database
public void deleteProduct(String productName) {
SQLiteDatabase db = getWritableDatabase();
db.execSQL("DELETE FROM " + TABLE_PRODUCTS + " WHERE " + COLUMN_PRODUCTNAME +
" ="" + productName + "";");
}
//print out the database as a string
public String databaseToString() {
String dbString = "";
SQLiteDatabase db = getWritableDatabase();
//the tutorial guy did a "WHERE 1" instead of the ";" but that also does not work
String query = "SELECT * FROM " + TABLE_PRODUCTS + ";";
Cursor c = db.rawQuery(query, null);
c.moveToFirst();
while(!c.isAfterLast()) {
if(c.getString(c.getColumnIndex("_productname")) != null) {
dbString += c.getString(c.getColumnIndex("_productname"));
dbString += "n";
}
}
c.close();
db.close();
return dbString;
}
}

xml文件中最重要的部分:


<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:hint="@string/edittexthint"
android:importantForAutofill="no"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:onClick="insertButtonClicked"
android:text="@string/insert_msg"
app:layout_constraintStart_toStartOf="@+id/editText"
app:layout_constraintTop_toBottomOf="@+id/editText" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:onClick="deleteButtonClicked"
android:text="@string/delete_msg"
app:layout_constraintEnd_toEndOf="@+id/editText"
app:layout_constraintTop_toBottomOf="@+id/editText" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="@string/tv_content"
app:layout_constraintTop_toBottomOf="@+id/button"
app:layout_constraintStart_toStartOf="@+id/button"/>
</androidx.constraintlayout.widget.ConstraintLayout>

这段代码中的一个问题可能是我在SQL语句中添加了不必要的分号
我取得了一些进展,但使用了一种不同且更简单的方法,没有为表提供类

databaseToString()方法中,在while(!c.isAfterLast())循环中读取光标时,忘记使用c.moveToNext()将光标移动到新行。因此,while循环从未终止。

最新更新