没有这样的列:R1.ReadingDate错误

时间:2014-01-25 10:56:23

标签: android sqlite

我使用sqlite manager在sqlite中创建了一个视图。

Create View vMeterReading
as
SELECT M._id as Meter_id, M.MeterNumber, R1.ReadingDate, R1.Reading AS              CurrentReading, R2.ReadingDate AS PrevReadingDate, R2.Reading AS PrevMeterReading, R2.Rate  as Rate, R2._id,R1.TenantMeter_id
FROM (Meters AS M INNER JOIN TenantMeters ON M._id = TenantMeters.Meter_id) INNER JOIN (MeterReading AS R1 INNER JOIN MeterReading AS R2 ON R1.TenantMeter_id = R2.TenantMeter_id) ON TenantMeters._id = R1.TenantMeter_id
WHERE (((R2.ReadingDate)=(SELECT Max(R3.ReadingDate) 
      FROM [MeterReading] AS R3   
      WHERE (R3.TenantMeter_id = R1.TenantMeter_id)   
        AND (R3.ReadingDate < R1.ReadingDate)     
    ))) OR (((R2.TenantMeter_id) Is Null))
ORDER BY M.MeterNumber, R1.ReadingDate.

我试图在android中使用这个视图(select * from vMeterReading),我得到了标题错误。

我还从命令提示符连接到sqlite db并尝试执行相同的查询。我得到了同样的错误。

sql语句有什么问题?

在sql manager(firefox附加组件)中运行良好。

2 个答案:

答案 0 :(得分:0)

SQLite不保证表名前缀(如R1.1)是否是结果列名的一部分。

您应该使用AS明确地为所有视图的列命名。

答案 1 :(得分:0)

问题是android使用的SQLite版本与sQLite管理器使用的版本不同。 Android版本不支持我正在使用的连接。 下面的查询执行时没有问题。 从此网站获得此建议http://sqlite.1065341.n5.nabble.com/

SELECT M._id as Meter_id, M.MeterNumber, R1.ReadingDate as ReadingDate,
R1.Reading AS CurrentReading, R2.ReadingDate AS PrevReadingDate, R2.Reading
AS PrevMeterReading, R2.Rate as Rate, R2._id as _id,R1.TenantMeter_id as TenantMeterID
FROM Meters AS M INNER JOIN TenantMeters ON M._id = TenantMeters.Meter_id
INNER JOIN MeterReading AS R1 ON R1.TenantMeter_id = TenantMeters ._id
 INNER JOIN MeterReading AS R2 ON
R1.TenantMeter_id = R2.TenantMeter_id 

WHERE (((R2.ReadingDate)=(SELECT Max(R3.ReadingDate)
      FROM [MeterReading] AS R3
      WHERE (R3.TenantMeter_id = R1.TenantMeter_id)
        AND (R3.ReadingDate < R1.ReadingDate)
    ))) OR (((R2.TenantMeter_id) Is Null))