当前位置: 首页 > 知识库问答 >
问题:

每当我尝试将数据输入数据库时(在android studio中),我都会收到一个sqlite错误代码1

杜烨伟
2023-03-14

我已经创建了一个数据库助手类,当我运行代码时,我不断收到以下错误消息:“android.database.sqlite.SQLiteException:table words_info没有名为back_word的列(代码1 sqlite_error[1]):,编译时:INSERT INTO words_info(front_word,back_word)值(?,)。”我试图更改数据库的版本,但也没用。。。在Android。数据库sqlite。SQLiteConnection。nativePrepareStatement(本机方法)

package com.example.language;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

public class HelperDB extends SQLiteOpenHelper {
    static final int DATABASE_VERSION = 1;
    public static final String DB_NAME = "words.db";
    public static final String TABLE_NAME = "words_info";
    public static final String FRONT_WORD = "front_word";
    public static final String BACK_WORD = "back_word";




    public HelperDB(Context context) {
        super(context, DB_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        String st = "CREATE TABLE " + TABLE_NAME + " ( " + FRONT_WORD + " TEXT , " + BACK_WORD + "TEXT ) "; //CREATE TABLE words_info (front_word TEXT, back_word TEXT)
        db.execSQL(st);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);

        // Create tables again
        onCreate(db);
    }

    public void addWord(Card c){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues cv = new ContentValues();

        cv.put(FRONT_WORD, c.getFront_word());
        cv.put(BACK_WORD, c.getBack__word());

        db.insert(TABLE_NAME, null, cv);
        db.close();
    }

    public List<Card> getWords(){
        List<Card> returnList = new ArrayList<>();

        String queryString = "SELECT * FROM " + TABLE_NAME;

        SQLiteDatabase db = getReadableDatabase();

        Cursor cursor = db.rawQuery(queryString, null);

        if(cursor.moveToFirst()){
            //loop through the cursor (result set) and create new card object. put them into the return list
            do{
                String front_word = cursor.getString(0);
                String back_word = cursor.getString(1);

                Card card = new Card(front_word, back_word);
                returnList.add(card);

            }while(cursor.moveToNext());
        }
        else{

        }
        cursor.close();
        return returnList;
    }

    public void deleteOne(Card c){
        SQLiteDatabase db = getWritableDatabase();
        String queryString = "DELETE FROM " + TABLE_NAME + " WHERE " + FRONT_WORD + " = " + c.getFront_word();

        Cursor cursor = db.rawQuery(queryString, null);

    }


} 
2022-05-01 16:15:56.733 2853-2853/com.example.language E/SQLiteLog: (1) table words_info has no column named back_word in "INSERT INTO words_info(front_word,back_word) VALUES (?,?)"
2022-05-01 16:15:56.737 2853-2853/com.example.language E/SQLiteDatabase: Error inserting front_word=hello back_word=hello
    android.database.sqlite.SQLiteException: table words_info has no column named back_word (code 1 SQLITE_ERROR[1]): , while compiling: INSERT INTO words_info(front_word,back_word) VALUES (?,?)
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1463)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:901)
        at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:590)
        at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:62)
        at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:33)
        at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:2217)
        at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:2088)
        at com.example.language.HelperDB.addWord(HelperDB.java:51)
        at com.example.language.MainActivity$1.onClick(MainActivity.java:50)
        at android.view.View.performClick(View.java:8160)
        at android.widget.TextView.performClick(TextView.java:16222)
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
        at android.view.View.performClickInternal(View.java:8137)
        at android.view.View.access$3700(View.java:888)
        at android.view.View$PerformClick.run(View.java:30236)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:246)
        at android.app.ActivityThread.main(ActivityThread.java:8595)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)

共有1个答案

董凡
2023-03-14

我认为你应该在第二个“文本”前面加一个空格

因此,您创建表的代码如下所示:

String st = "CREATE TABLE " + TABLE_NAME + " ( " + FRONT_WORD + " TEXT , " + BACK_WORD + " TEXT ) ";
 类似资料:
  • //回收器和视图 //调用提取数据方法extractData(); //提取数据

  • 我有这个问题。我正在尝试连接到数据库并推送文本用户。当我打开文件时,我只会发现很多错误。您可以进一步查看错误和我的代码。仅供参考:我的数据库在MySQLi中 警告:mysqli::\uuuu construct():php\u network\u getaddresses:getaddrinfo失败:第3行的/www/webvol9/rj/fxgnq6r66hz6x2j/my domain/pub

  • 进程 c:在函数“主要”中: prog.c:35:20:警告:格式“%d”需要类型为“int”的参数,但参数3的类型为“void * ”[-w format =]printf(" \ n % c \ t % d \ t identifier \ n ",c,p);^ prog. c: 47:24:警告:格式'%d'需要类型'int'的参数,但参数3的类型'void*'[-Wformat=]prin

  • 我正在为注释的项目添加jQuery autocomplete我需要标记(在中),但是当我添加它时,我得到以下错误:

  • 所以我想在一个控制器中创建两个存储。当我输入时,我确实返回到索引页面,但我提交的数据不存在。然后我看到了我的本地主机,但是我输入的数据也不在那里。当我输入时,没有错误消息。所以我不知道我的错误在哪里。 这是我的看法 createincome.blade.php 这是我的控制器 财务控制人。php 这是我的路线 web.php

  • 我无法将表格发送到数据库。我已经试了一个小时来解决这个问题;我用“echo mysqli_error();”将代码打了个谜,但并没有收到任何错误,只是数据库中缺少新数据(似乎提交表单也并没有重新加载页面来发送信息)。数据库使用config.php、connectDB.php和header.php为我的其余页面工作,因此问题不在数据库设置中。我相信错误在“isset($\u POST['submit