Android SQLite默认值
我想让用户在定义要创建的表的列时输入一个字符串,比如“TEST” 让列名为“标准” 在将行插入到创建的表中时,用户不必再次输入“TEST” 因此,此参数是否会起作用: 创建表tablename(…,标准文本默认值“TEST\”) ?Android SQLite默认值,android,sqlite,Android,Sqlite,我想让用户在定义要创建的表的列时输入一个字符串,比如“TEST” 让列名为“标准” 在将行插入到创建的表中时,用户不必再次输入“TEST” 因此,此参数是否会起作用: 创建表tablename(…,标准文本默认值“TEST\”) ? 请让我知道,因为我搜索了一段时间,无法澄清我的疑问为什么在创建字符串中使用\'\'? 以下工作: private static final String CREATE_TABLE_CONTACT = CREATE_TABLE + CONTACT.TABLE_NAME
请让我知道,因为我搜索了一段时间,无法澄清我的疑问为什么在创建字符串中使用
\'\'
?
以下工作:
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
为什么在创建字符串中使用
\'\'
以下工作:
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
你查过了吗?我查过了,但仍然不确定。你查过了吗?我查过,但仍然不确定