如何更新我的字符串值转换成数据库 [英] How to update my string value into database

查看:147
本文介绍了如何更新我的字符串值转换成数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与表名称状态和列名称的数据库是列1 。我需要更新此。

我在活动1

一个字符串值

 字符串检查=值;
 

我已经过了这个值进入我的 DBHelper 。我已经试过这样

  dbhelper =新DBHelper(本);
dbhelper.sample(检查);
 

我在 DBHelpe r.here此值。像这样

 公共无效样本(字符串PRMS){
Log.d(sucess,PRMS);
}
 

现在我怎么需要更新字符串PRMS 到我的数据库的ColumnName 列1

我已经试过这样

 公共无效样本(字符串PRMS){
        Log.d(DBHELPER成功,PRMS);

        尝试{
            SQLiteDatabase DB1 = this.getWritableDatabase();
            db1.execSQL(更新appstatus设置状态='+ PRMS +'WHERE ID = 1);
        }赶上(例外五){
            的System.out.println(GET采样值+ E);
        }
    }
 

有什么毛病我的语法?如何实现这一目标?

它显示异常,如

  12月2日至28号:09:45.604:我/的System.out(4975):GET样品VALUEandroid.database.sqlite.SQLiteException:表报告已经存在(code 1 ):在编制:创建表报告(级文本,主题文本,文本开始,结束文字,日期文字)
 

解决方案

rawQuery()不实际运行的SQL。使用 execSQL()来代替:

  db.execSQL(更新状态设置列1 ='+ PRMS +');
 

(见<一href="http://stackoverflow.com/questions/20110274/what-is-the-correct-way-to-do-inserts-updates-deletes-in-android-sqlitedatabase/20118910#20118910">What是做插入/更新/使用查询字符串的Andr​​oid SQLiteDatabase删除?了解详情如何 rawQuery()的正确方法execSQL()引擎盖下工作。)

Have a database with table name status and column name is column1. I need to update this.

I have a String value in Activity1

String check = "value";

I have passed this value into my DBHelper.I have tried like this

dbhelper = new DBHelper(this);
dbhelper.sample(check);

I got this value in DBHelper.here. Like this

public void sample(String prms){
Log.d("sucess",prms);
}

Now how i need to update String prms into my database columnname column1

I have tried like this

public  void sample( String prms) {
        Log.d("DBHELPER SUCCESS", prms);

        try{
            SQLiteDatabase db1 = this.getWritableDatabase(); 
            db1.execSQL("update appstatus SET status = '"+prms+"' WHERE id = 1 ");
        }catch(Exception e){
            System.out.println("GET SAMPLE VALUE"+e);
        }
    }

What's wrong with my syntax? How to achieve this?

It shows exception as

02-28 12:09:45.604: I/System.out(4975): GET SAMPLE VALUEandroid.database.sqlite.SQLiteException: table report already exists (code 1): , while compiling: create table report(level TEXT, topic TEXT,  start TEXT, end TEXT, date TEXT)

解决方案

rawQuery() doesn't actually run the SQL. Use execSQL() instead:

db.execSQL("update status SET column1 = '"+prms+"' ");

(See What is the correct way to do inserts/updates/deletes in Android SQLiteDatabase using a query string? to learn more how rawQuery() and execSQL() work under the hood.)

这篇关于如何更新我的字符串值转换成数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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