我需要检查列是否存在,如果不存在则添加它.根据我的研究,看起来sqlite不支持IF语句,而应该使用case语句.
这是我到目前为止:
SELECT CASE WHEN exists(select * from qaqc.columns where Name = "arg" and Object_ID = Object_ID("QAQC_Tasks")) = 0 THEN ALTER TABLE QAQC_Tasks ADD arg INT DEFAULT(0);
Run Code Online (Sandbox Code Playgroud)
但我收到错误:"ALTER"附近:语法错误.
有任何想法吗?
Rah*_*thi 49
你不能用ALTER TABLE with例.
您正在寻找获取表的列名:: -
PRAGMA table_info(table-name);
Run Code Online (Sandbox Code Playgroud)
在PRAGMA上查看本教程
该pragma为命名表中的每列返回一行.结果集中的列包括列名,数据类型,列是否可以为NULL以及列的默认值.对于不属于主键的列,结果集中的"pk"列为零,并且是主键中作为主键一部分的列的列的索引.
小智 16
// This method will check if column exists in your table
public boolean isFieldExist(String tableName, String fieldName)
{
boolean isExist = false;
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("PRAGMA table_info("+tableName+")",null);
res.moveToFirst();
do {
String currentColumn = res.getString(1);
if (currentColumn.equals(fieldName)) {
isExist = true;
}
} while (res.moveToNext());
return isExist;
}
Run Code Online (Sandbox Code Playgroud)
For*_*ntz 14
虽然这是一个老问题,我在PRAGMA函数中找到了一个更简单的解决方案:
SELECT COUNT(*) AS CNTREC FROM pragma_table_info('tablename') WHERE name='column_name'
Run Code Online (Sandbox Code Playgroud)
如果结果大于零,则列存在.简单和一行查询
诀窍是使用
pragma_table_info('tablename')
Run Code Online (Sandbox Code Playgroud)
代替
PRAGMA table_info(tablename)
Run Code Online (Sandbox Code Playgroud)
编辑:请注意,正如PRAGMA函数中所报告的:
此功能是实验性的,可能会发生变化.如果正式支持PRAGMA功能的表值函数,则可以使用更多文档.
在SQLite版本3.16.0(2017-01-02)中添加了PRAGMA功能的表值函数.SQLite的早期版本无法使用此功能.
您没有指定语言,因此假设它不是纯 sql,您可以检查列查询中的错误:
SELECT col FROM table;
Run Code Online (Sandbox Code Playgroud)
如果您收到错误,则您知道该列不存在(假设您知道该表存在,无论如何您对此都有“IF NOT EXISTS”),否则该列存在,然后您可以相应地更改该表。
小智 5
我已应用此解决方案:
public boolean isFieldExist(SQLiteDatabase db, String tableName, String fieldName)
{
boolean isExist = false;
Cursor res = null;
try {
res = db.rawQuery("Select * from "+ tableName +" limit 1", null);
int colIndex = res.getColumnIndex(fieldName);
if (colIndex!=-1){
isExist = true;
}
} catch (Exception e) {
} finally {
try { if (res !=null){ res.close(); } } catch (Exception e1) {}
}
return isExist;
}
Run Code Online (Sandbox Code Playgroud)
它是Pankaj Jangid的代码变体.
对任何 rawQuery() 执行使用 try、catch 和 finally 以获得更好的实践。下面的代码会给你结果。
public boolean isColumnExist(String tableName, String columnName)
{
boolean isExist = false;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = null;
try {
cursor = db.rawQuery("PRAGMA table_info(" + tableName + ")", null);
if (cursor.moveToFirst()) {
do {
String currentColumn = cursor.getString(cursor.getColumnIndex("name"));
if (currentColumn.equals(columnName)) {
isExist = true;
}
} while (cursor.moveToNext());
}
}catch (Exception ex)
{
Log.e(TAG, "isColumnExist: "+ex.getMessage(),ex );
}
finally {
if (cursor != null)
cursor.close();
db.close();
}
return isExist;
}
Run Code Online (Sandbox Code Playgroud)
小智 5
SELECT EXISTS (SELECT * FROM sqlite_master WHERE tbl_name = 'TableName' AND sql LIKE '%ColumnName%');
Run Code Online (Sandbox Code Playgroud)
..请注意 LIKE 条件是不完美的,但它对我有用,因为我所有的列都有非常独特的名称..
| 归档时间: |
|
| 查看次数: |
63400 次 |
| 最近记录: |