插入SQLite数据库android [英] Insert in SQLite Database android

查看:46
本文介绍了插入SQLite数据库android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Android 新手.我在数据库中的插入语句遇到了一些问题,当我运行应用程序时,值没有被插入.请有人可以帮忙..

im new on Android. I have some trouble with the insert statement in the database, when im running the application the values have not been inserted. Please someone can help..

public class DatabaseAdapter extends SQLiteOpenHelper {
// Database attributes
public static final String DB_NAME = "MoneyManagerSystemTr";
public static final int DB_VERSION = 1;

// Table attributes

public static final String TABLE_ACCOUNT = "account_table";

//Account Table
public static final String KEY_BANKNAME ="bankname";
public static final String KEY_TYPE = "type";
public static final String KEY_ACCNUM = "accnum";
public static final String KEY_BALANCE = "balance";
public static final String KEY_EXPIRYDATE = "expirydate";

@Override
public void onCreate(SQLiteDatabase db) {


    String AccountTable = "create table if not exists " + TABLE_ACCOUNT + " ( " + BaseColumns._ID + " integer primary key autoincrement, " 
            + KEY_BANKNAME + " text not null, "
            + KEY_TYPE + " text, "
            + KEY_ACCNUM + " text, "
            + KEY_BALANCE + " text, "
            + KEY_EXPIRYDATE + " text);";

    db.execSQL(AccountTable);

String ROW1 = "INSERT INTO " + TABLE_ACCOUNT + " Values ('Cash','','',0, '');";
    db.execSQL(ROW1);

    String ROW2 = "INSERT INTO " + TABLE_ACCOUNT + " Values ('Bank Account','','',0, '');";
    db.execSQL(ROW2);

    String ROW3 = "INSERT INTO " + TABLE_ACCOUNT + " Values ('Credit Card','','',0, '');";
    db.execSQL(ROW3);

推荐答案

从插入语句中删除分号并在 0 周围添加引号:

Remove the semicolons from your insert statements and add quotes around 0:

String ROW1 = "INSERT INTO " + TABLE_ACCOUNT + " ("
              + KEY_BANKNAME + ", " + KEY_TYPE + ", "
              + KEY_ACCNUM + ", " + KEY_BALANCE + ", "
              + KEY_EXPIRYDATE + ") Values ('Cash', '', '', '0', '')";
db.execSQL(ROW1);

更好的是,请注意 execSQL() 并使用 insert() 代替.

Better yet, heed the suggestion at execSQL() and use insert() instead.

这篇关于插入SQLite数据库android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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