0

我想让用户输入一个字符串,在定义要创建的表的列时说“TEST”。

让列名称为“标准”

用户在向创建的表中插入行时永远不必再次输入“TEST”。

这个参数也可以完成这项工作:

create table tablename( ..... , 标准文本默认\'TEST\');

?

请让我知道,因为我搜索了一段时间,但无法消除我的疑问

4

1 回答 1

4

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

以下作品:

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 的完整示例:

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
于 2013-03-11T15:06:42.650 回答