我有三个表(IncidentCategory,IncidentQuestion,IncidentOption),我需要触发一个连接查询来排列和选择一些数据。同样,一些SQL Server开发人员编写了如下所示的一个查询:
服务器查询
SELECT * FROM [IncidentQuestion] AS IQ WHERE IQ.[IncidentCategoryId]=27 AND
IQ.[IsOption]=0 AND (SELECT COUNT(IO.[Id]) FROM [dbo]. [IncidentOption] AS
IO WHERE IO.[IncidentQuestionId]=IQ.[Id])<>0 ORDER BY IQ.[OrderId] ASC;
DECLARE @False BIT; SET @False=0;SELECT IP.*,COALESCE(IV.[Id], 0) AS
[IncidentValueId],COALESCE(IV.[IsChecked], @False) AS [IsChecked],IV.
[Value],IV.IncidentId FROM [IncidentOption] AS IP LEFT JOIN (SELECT * FROM
[IncidentValue] WHERE [IncidentId]=0) AS IV ON IP.[Id]=IV.[IncidentOptionId]
WHERE IP.[IncidentQuestionId]=68 ORDER BY IP.[OrderId] ASC;
在我的应用程序中,我创建了与服务器中列名相同的表。
IncidentQuestion DbHelper:
private static final String INCIDENT_QUESTION_TABLE = "IncidentQuestionTable";
private static final String ID = "_id";
private static final String SERVER_ID = "Id";
private static final String CONTRACT_TYPE_ID = "ContractTypeId";
private static final String INCIDENT_CATAGORY_ID = "IncidentCategoryId";
private static final String QTEXT = "QText";
private static final String TYPE = "Type";
private static final String ORDER_ID = "OrderId";
private static final String IS_MANDATORY = "IsMandatory";
private static final String IS_OPTION = "IsOption";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "IncidentQuestion";
private final Context context;
private SQLiteDatabase ourDatabase;
private DbHelper ourHelper;
public class DbHelper extends SQLiteOpenHelper {
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String locationQuery = "CREATE TABLE " + INCIDENT_QUESTION_TABLE + " ("
+ ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ SERVER_ID + " TEXT NOT NULL, " + CONTRACT_TYPE_ID
+ " TEXT NOT NULL, " + INCIDENT_CATAGORY_ID
+ " TEXT NOT NULL, " + QTEXT
+ " TEXT NOT NULL, " + TYPE
+ " TEXT NOT NULL, " + ORDER_ID
+ " TEXT NOT NULL, " + IS_MANDATORY
+ " TEXT NOT NULL, " + IS_OPTION + " TEXT NOT NULL);";
db.execSQL(locationQuery);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + INCIDENT_QUESTION_TABLE);
onCreate(db);
}
}
IncidentOption DbHelper:
private static final String INCIDENT_OPTION_TABLE = "IncidentOptionTable";
private static final String ID = "_id";
private static final String SERVER_ID = "Id";
private static final String INCIDENT_QUESTION_ID = "IncidentQuestionId";
private static final String OPTION_TEXT = "OptionText";
private static final String ORDER_ID = "OrderId";
private static final String PARENT_ID = "ParentId";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "IncidentOption";
private final Context context;
private SQLiteDatabase ourDatabase;
private DbHelper ourHelper;
public class DbHelper extends SQLiteOpenHelper {
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String locationQuery = "CREATE TABLE " + INCIDENT_OPTION_TABLE + " ("
+ ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ SERVER_ID + " TEXT NOT NULL, "
+ INCIDENT_QUESTION_ID + " TEXT NOT NULL, " + OPTION_TEXT
+ " TEXT NOT NULL, " + ORDER_ID
+ " TEXT NOT NULL, " + PARENT_ID + " TEXT NOT NULL);";
db.execSQL(locationQuery);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + INCIDENT_OPTION_TABLE);
onCreate(db);
}
}
所有选择,插入,创建,删除查询都运行正常,我无法转换并在我的本地数据库中进行服务器查询。可以请任何人帮我找出如何在我的本地数据库中编写服务器查询的方式或者我将如何编写一些像服务器查询一样工作的查询。建议大多是值得注意的。
答案 0 :(得分:0)
SQLite支持[括号]以与SQL Server兼容;你不需要删除它们。
SQLite没有DECLARE。
在显示的查询中,False
是常量零(这也是SQLite处理布尔值的方式);只需删除DECLARE和SET,然后将@False
替换为0
。