Android的SQLite的默认值 [英] Android SQLite Default Value

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

问题描述

我想让用户输入一个字符串,说TEST,而定义表的列创建。

I want to make the user enter a String, say "TEST" while defining the columns of a table to be created.

让列名是标准

该用户永远不应该同时将行插入到所创建的表输入test了。

The user should never have to enter "TEST" again while inserting rows into the created Table.

所以将这个参数做的工作:

so will this parameter do the job :

创建表的表名(.....,标准文本默认的\\TEST \\');

create table tablename( ..... , standard text default \'TEST\');

请让我知道,因为我搜索了一会儿,不能让我的怀疑清零

Please let me know, as I searched a while and could not get my doubt cleared

推荐答案

为什么要使用 \\'\\'在创建字符串?

Why do you use \' \' in creation string?

以下工作:

private static final String CREATE_TABLE_CONTACT = CREATE_TABLE + CONTACT.TABLE_NAME + " (" + CONTACT._ID+ " INTEGER PRIMARY KEY," + CONTACT.FIRST_NAME + " TEXT, " + CONTACT.LAST_NAME + " TEXT DEFAULT 'TEST');";

下面是DBHelper的完整的例子:

Here is full example of DBHelper:

public class DBHelper extends SQLiteOpenHelper {


public static interface CONTACT extends BaseColumns {
                String TABLE_NAME = "human";
                String FIRST_NAME = "first_name";
                String LAST_NAME = "last_name";
            }

  private static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS ";
            private static final String DATABASE_NAME = "someDataBase";
            private static final int DATABASE_VERSION = 1;


  private static final String CREATE_TABLE_CONTACT = CREATE_TABLE + CONTACT.TABLE_NAME + " (" + CONTACT._ID
                    + " INTEGER PRIMARY KEY," + CONTACT.FIRST_NAME + " TEXT, " + CONTACT.LAST_NAME
                    + " TEXT DEFAULT 'TTEESSTT');"; 

 public DBHelper(Context context) {
                super(context, DATABASE_NAME, null, DATABASE_VERSION);
            }

   @Override
            public void onCreate(SQLiteDatabase db) {
                db.execSQL(CREATE_TABLE_CONTACT);
            }

@Override
            public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
                db.execSQL("DROP TABLE IF EXISTS " + CONTACT.TABLE_NAME);
                onCreate(db);
}

public Cursor getAllContacts() {
                return getReadableDatabase().query(CONTACT.TABLE_NAME, null, null, null, null, null, null);
            }


//method to insert contact
            public void insertContact(String firstName, String lastName) {
                final ContentValues values = new ContentValues();
                values.put(CONTACT.FIRST_NAME, firstName);
                //optional parameter 
                if (lastName != null) {
                    values.put(CONTACT.LAST_NAME, lastName);
                }
                getWritableDatabase().insert(CONTACT.TABLE_NAME, null, values);
            }
        }

And its usage, for example:

DBHelper db = new DBHelper(this);
            db.insertContact("Dmitry", "Antonov");
            db.insertContact("Andy", "Sudorov");
            db.insertContact("Oleg", null);
            db.insertContact("Ivan", "Lozin");
            db.insertContact("Serg", null);
            Cursor allContacts = db.getAllContacts();
            ListView testList = (ListView) findViewById(R.id.testList);
            testList.setAdapter(new SimpleCursorAdapter(this, android.R.layout.two_line_list_item, allContacts,
                    new String[] { DBHelper.CONTACT.FIRST_NAME, DBHelper.CONTACT.LAST_NAME }, new int[] {android.R.id.text1, android.R.id.te

这篇关于Android的SQLite的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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