[Android Studio-SQLite插入冻结

问题描述 投票:1回答:2

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

这是我的不带导入的MainActivity:

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>
android sqlite insert freeze
2个回答
0
投票

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


0
投票

databaseToString()循环中读取光标的while(!c.isAfterLast())方法中,您忘记了使用c.moveToNext()将光标移动到新行。结果,while循环永远不会终止。

© www.soinside.com 2019 - 2024. All rights reserved.