在安卓应用中无法在表中插入数值(SQLite)

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

我正面临一个非常奇怪的问题,我真的看不出是什么原因造成的,所以我正试图在一个表中插入值,首先让我告诉你这个表是存在的(我已经检查了它的Db Browser for SQLite),这个问题让我硬编码插入查询,但没有任何变化。为了节省空间,我将只粘贴必要的代码块。所以让我给你看看代码块。

public class DBHelper extends SQLiteOpenHelper {

    public static final int DATABASE_VERSION = 3;
    public static final String DATABASE_NAME = "SemsWMS.db";

    public DBHelper(Context context) {

        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }



    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(createCompany);
        db.execSQL(createFinDoc);
        db.execSQL(createMtrLines);
        db.execSQL(createTrdr);
        db.execSQL(createTrdBranch);
        db.execSQL(createMtrl);
        db.execSQL(createMtrLot);
        db.execSQL(createMtrSubstitute);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        db.execSQL("drop table if exists company");
        db.execSQL("drop table if exists findoc");
        db.execSQL("drop table if exists mtrlines");
        db.execSQL("drop table if exists trdr");
        db.execSQL("drop table if exists trdbranch");
        db.execSQL("drop table if exists mtrl");
        db.execSQL("drop table if exists mtrlot");
        db.execSQL("drop table if exists mtrsubstitute");

        db.execSQL(createCompany);
        db.execSQL(createFinDoc);
        db.execSQL(createMtrLines);
        db.execSQL(createTrdr);
        db.execSQL(createTrdBranch);
        db.execSQL(createMtrl);
        db.execSQL(createMtrLot);
        db.execSQL(createMtrSubstitute);

    }

public static class Company implements BaseColumns{
        public static final String tablename = "company";
        public static final String company = "company";
        public static final String site = "site";
        public static final String webuser = "webuser";
        public static final String password = "password";
        public static final String appid = "appid";
        public static final String webuserid = "webuserid";
        public static final String upddatetrdr = "upddatetrdr";
        public static final String upddatetrdbranch = "upddatetrdbranch";
        public static final String upddatemtrl = "upddatemtrl";
        public static final String upddatemtrlot = "upddatemtrlot";
        public static final String upddatemtrsubstitute = "upddatemtrsubstitute";
    }

public static final String createCompany = "create table " + Company.tablename + "("+Company.company+" integer primary key, "
            +Company.site+" text, "
            +Company.webuser+" text, "
            +Company.password+" text, "
            +Company.appid +" integer, "
            +Company.webuserid+" integer, "
            +Company.upddatetrdr+" numeric, "
            +Company.upddatetrdbranch+" numeric, "
            +Company.upddatemtrl+" numeric, "
            +Company.upddatemtrlot+" numeric, "
            +Company.upddatemtrsubstitute+" numeric)";


DBHelper类中的硬编码块

 public static  String insertCompany(){
        String sql = "insert into company(company,site,appid,webuser,password) values(2,rigas,9999,sems,1234)";



        return sql;
    } 

DBHelper类中的方法,以便在其他活动中执行查询。

public void insertInDB(String query){

        SQLiteDatabase db = getWritableDatabase();
        db.execSQL(query);
    }

所以在其他活动中,我调用insertInDB(insertCompany());然后我得到这个。

 android.database.sqlite.SQLiteException: no such column: rigas (code 1): , while compiling: insert into company(company,site,appid,webuser,password) values(2,rigas,9999,sems,1234)
    #################################################################
    Error Code : 1 (SQLITE_ERROR)
    Caused By : SQL(query) error or missing database.
        (no such column: rigas (code 1): , while compiling: insert into company(company,site,appid,webuser,password) values(2,rigas,9999,sems,1234))
    #################################################################
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1073)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:638)

它说列 "rigas"......但在我的插入查询中没有这个列。... "rigas "是列 "site "的一个值。

如果有人能在代码中找到一些东西,我将非常感激。

android sqlite android-sqlite sql-insert
1个回答
0
投票

在一个有效的插入语句中,所有的字符串字符必须用单引号括起来。所以你的语句中的硬编码值应该是。

insert into company(company,site,appid,webuser,password) values(2,'rigas',9999,'sems',1234)

如果你不使用单引号,那么...... rigassem 被当作列名来处理,因为它们不是,所以你会得到这个错误。

但是正确的、安全的、推荐的在表中插入新行的方法是使用 insert() 办法,与 ContentValues,像这样。

public int insertInDB(String site, int appid, String webuser, String password) {
    SQLiteDatabase db = getWritableDatabase();
    ContentValues c = new ContentValues();
    c.put(Company.site, site);
    c.put(Company.appid, appid);
    c.put(Company.webuserid, webuser);
    c.put(Company.password, password);
    int result = db.insert(Company.tablename, null, c);
    db.close();
    return result;
}  

所以你传给 insertInDB() 4列值(不需要通过 company 既然你把它定义为 integer primary key 那么它就是AUTOINCREMENT)。你可以检查 insertInDB()如果是: -1 那么插入失败。其他的内容将是新行的id(列的id)。company),意味着它成功了。

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