在编译时,:插入或更换INTO空":语法错误; SQLiteException附近&QUOT [英] SQLiteException near "null": syntax error: , while compiling: INSERT OR REPLACE INTO

查看:219
本文介绍了在编译时,:插入或更换INTO空":语法错误; SQLiteException附近&QUOT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想配置简单DbHelper,但在执行得到一个错误。

该错误是:

  SQLiteException附近的空:语法错误:,在编译:插入或更换INTO

我也弄不清是什么问题?

该DbHelper类是:

 包com.sqlite.test1;    进口android.content.Context;
    进口android.database.sqlite.SQLiteDatabase;
    进口android.database.sqlite.SQLiteOpenHelper;    公共类DbHelper扩展SQLiteOpenHelper {
    公共静态最终诠释DB_VERSION = 1;
    公共静态最后弦乐DB_NAME =time_storage;
    公共静态最后弦乐DB_TABLE =timer_data;    公共静态最后弦乐C_ID =iderty_id;
    公共静态最后弦乐C_DATE =日期;
      上下文语境;    公共DbHelper(上下文的背景下){
        超(背景下,DB_NAME,空,DB_VERSION);
        this.context =背景;
    }    @覆盖
    公共无效的onCreate(SQLiteDatabase DB){
        db.execSQL(CREATE TABLE如果不存在+ DB_TABLE +(+ C_ID +整数主键自动增量
                    + C_DATE +文字NOT NULL););    }    @覆盖
    公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){
        db.execSQL(DROP TABLE IF EXISTS+ DB_TABLE);
        this.onCreate(DB);
    }}

的主类是:

 包com.sqlite.test1;进口android.app.Activity;
进口android.content.ContentValues​​;
进口android.database.sqlite.SQLiteDatabase;
进口android.os.Bundle;公共类SQlite_test1Activity延伸活动{
/ **当第一次创建活动调用。 * /
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);    DbHelper dbHelper =新DbHelper(SQlite_test1Activity.this);
    SQLiteDatabase分贝= dbHelper.getWritableDatabase();    字符串变量1 =这是第一个文本数据库;
    ContentValues​​值=新ContentValues​​();
    db.insertWithOnConflict(DbHelper.DB_TABLE,空,价值观,
            SQLiteDatabase.CONFLICT_REPLACE);
    values​​.put(DbHelper.C_DATE,变量1);    db.close();
    dbHelper.close();}}


解决方案

您需要把这些值你插入之前,而不是之后,否则你不插入任何内容。更改此:

  ContentValues​​值=新ContentValues​​();
db.insertWithOnConflict(DbHelper.DB_TABLE,空,价值观,
        SQLiteDatabase.CONFLICT_REPLACE);
values​​.put(DbHelper.C_DATE,变量1);

这样:

  ContentValues​​值=新ContentValues​​();
values​​.put(DbHelper.C_DATE,变量1);
db.insertWithOnConflict(DbHelper.DB_TABLE,空,价值观,
        SQLiteDatabase.CONFLICT_REPLACE);

修改

发现了另一个问题:

  db.execSQL(如果不存在创建表+ DB_TABLE +(+ C_ID +整数主键自动增量+ C_DATE +文字NOT NULL);) ;

以上code为创建一个名为一个主键 iderty_idinteger 。它应该是这样的:

  db.execSQL(如果不存在创建表+ DB_TABLE +(+ C_ID +整数主键自动增量+ C_DATE +文字NOT NULL);) ;

请注意前插入空格整数主 ...

I am trying to configure a simple DbHelper, and getting an error while executing.

The error is:

 SQLiteException near "null": syntax error: , while compiling: INSERT OR REPLACE INTO

I cannot figure out what is the problem?

The DbHelper class is:

    package com.sqlite.test1;

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

    public class DbHelper extends SQLiteOpenHelper{


    public static final int DB_VERSION = 1;
    public static final String DB_NAME = "time_storage";
    public static final String DB_TABLE = "timer_data";

    public static final String C_ID = "iderty_id";
    public static final String C_DATE = "date";
      Context context;

    public DbHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
        this.context = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table if not exists " + DB_TABLE + " (" + C_ID + "integer primary key autoincrement, " 
                    + C_DATE + " text not null );");

    }

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

The main class is:

package com.sqlite.test1;

import android.app.Activity;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;

public class SQlite_test1Activity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    DbHelper dbHelper = new DbHelper(SQlite_test1Activity.this);
    SQLiteDatabase db = dbHelper.getWritableDatabase();

    String variable1 = "this is the first text to database";
    ContentValues values = new ContentValues();
    db.insertWithOnConflict(DbHelper.DB_TABLE, null, values,
            SQLiteDatabase.CONFLICT_REPLACE);
    values.put(DbHelper.C_DATE, variable1);

    db.close();
    dbHelper.close();

}}

解决方案

You need to put in the values before you do the insert, not after, otherwise you're not inserting anything. Change this:

ContentValues values = new ContentValues();
db.insertWithOnConflict(DbHelper.DB_TABLE, null, values,
        SQLiteDatabase.CONFLICT_REPLACE);
values.put(DbHelper.C_DATE, variable1);

to this:

ContentValues values = new ContentValues();
values.put(DbHelper.C_DATE, variable1);
db.insertWithOnConflict(DbHelper.DB_TABLE, null, values,
        SQLiteDatabase.CONFLICT_REPLACE);

EDIT

Found another issue:

db.execSQL("create table if not exists " + DB_TABLE + " (" + C_ID + "integer primary key autoincrement, " + C_DATE + " text not null );");

The above code is creating a primary key called iderty_idinteger. It should be like this:

db.execSQL("create table if not exists " + DB_TABLE + " (" + C_ID + " integer primary key autoincrement, " + C_DATE + " text not null );");

Note the space inserted before integer primary...

这篇关于在编译时,:插入或更换INTO空":语法错误; SQLiteException附近&QUOT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆