【问题标题】:Problem with inserting data in database andoid studio在数据库android studio中插入数据的问题
【发布时间】:2021-08-21 17:10:18
【问题描述】:

我有一个相当小的应用程序,但它不工作


public class AddTemplateActivity extends AppCompatActivity {

    EditText heightET, descpET;
    Button addButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_template);

        heightET = (EditText) findViewById(R.id.etHeight);
        descpET = (EditText) findViewById(R.id.etDescription);

        addButton = (Button) findViewById(R.id.addButton);

        addButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyDataBaseHelper myDB = new MyDataBaseHelper(AddTemplateActivity.this);

                myDB.addTemplate(descpET.getText().toString(), Integer.valueOf(heightET.getText().toString()));
            }
        });
    }
}

这个活动应该在我的 SQLite 数据库中插入数据。我创建类


public class MyDataBaseHelper extends SQLiteOpenHelper {

    private Context context;
    private static final String DATABASE_NAME = "TemplatesLibrary.db";
    private static final int DATABASE_VERSION = 2;

    private static final String TABLE_NAME = "templates_categories";
    private static final String COLUMN_ID = "_id";
    private static final String COLUMN_DESCRIPTION = "template_description";
    private static final String COLUMN_HEIGHT = "template_height";

    public MyDataBaseHelper(@Nullable Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        this.context = context;
    }

    @Override
    public void onCreate(SQLiteDatabase database) {
        String query = "CREATE TABLE " + TABLE_NAME + " (" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_DESCRIPTION + " TEXT, " + COLUMN_HEIGHT + " INTEGER NOT NULL);";

        database.execSQL(query);
    }

    @Override
    public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
        database.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(database);
    }

    public void addTemplate(String description, int height) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues cv = new ContentValues();

        cv.put(COLUMN_DESCRIPTION, description);
        cv.put(COLUMN_HEIGHT, height);

        long res = db.insert(COLUMN_DESCRIPTION, null, cv);
        if(res == -1){
            Toast.makeText(context, "Failed", Toast.LENGTH_SHORT).show();
        }
        else{
            Toast.makeText(context, "Added!", Toast.LENGTH_SHORT).show();
        }
    }
}

但是当我点击按钮添加时,我收到了

android.database.sqlite.SQLiteException: no such table: template_description (code 1 SQLITE_ERROR): , while compiling: INSERT INTO template_description(template_height,template_description) VALUES (?,?)

请给我一个解决方案。谢谢!

【问题讨论】:

    标签: android database sqlite android-sqlite


    【解决方案1】:

    您需要将表而不是列名传递给insert()

    改变

    long res = db.insert(COLUMN_DESCRIPTION, null, cv);
    

    long res = db.insert(TABLE_NAME, null, cv);
    

    【讨论】:

    • 谢谢,我不能投你的票,因为我是新用户,我没有足够的声誉
    • 您可以点击对勾接受答案
    【解决方案2】:

    您的表名是:templates_categories,但在 addTemplate 方法中使用了 template_description

    也许你是一个初学者,也许你想了解 sqlite 和它的基础工作,但请继续使用 Room 来处理数据库

    【讨论】:

    • 谢谢,现在可以使用了!是的,我是 android 编程的初学者,这是我的第一次体验。我会试试 Room,谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多