日期为变量,sqlite按日期查询,字符怎么转化

lingshawu 2011-06-14 04:17:00
CTime tm;
m_datebegin.GetTime(tm);
CString strDate=tm.Format("%Y-%m-%d");
得到控件中的日期了(其中一个),要查询时间段内的数据,sql语句应该怎么写呢?主要是(where date >=什么 and date<=什么,什么,什么)
好像是这样吧。。。高手们,帮看看,新手刚学,不会转换,谢谢!
...全文
1109 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
karlson0211 2011-06-16
  • 打赏
  • 举报
回复
下面代码你看看:
UpdateData(true);
CString strd1,strd2;

strd1.Format("%d-%d-%d",m_t1.GetYear(),m_t1.GetMonth(),m_t1.GetDay());
strd2.Format("%d-%d-%d",m_t2.GetYear(),m_t2.GetMonth(),m_t2.GetDay());
CString str="select * from 表名 where 字段名 between '"+strd1+"' and '"+strd2+"'";

其中,数据库表中日期格式是:2001-6-7的文本型,m_t1,m_t2是日期控件,
karlson0211 2011-06-16
  • 打赏
  • 举报
回复
楼上正解,你用的是CString,说明你的数据库表字段类型也应该是字串
lingshawu 2011-06-16
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 karlson0211 的回复:]
你的字符串可能不对,
CString strsearchsql;
strsearchsql="select * from 表名 where "+ 字段1+ " between '" + 字段2 + "'";
[/Quote]
字段一是哪一项啊?
我用的是C++,两个时间控件,想要选定时间段后,点击“查看”。sqlite中要先转换成sql语句,转换这一步,总是转不出想要的,暂时只会整型转换,但是这样就只能显示2011,后面的日期就丢了,不能按想要的查看。
karlson0211 2011-06-16
  • 打赏
  • 举报
回复
你的字符串可能不对,
CString strsearchsql;
strsearchsql="select * from 表名 where "+ 字段1+ " between '" + 字段2 + "'";
lingshawu 2011-06-16
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 zyq5945 的回复:]
使用字符串就可以了,把它放在单引号之间。
[/Quote]
where date >=‘strDate’ ;
是这样吗?可是读取出来时,依然显示where date >=‘strDate’ ,时间没给反应啊
lingshawu 2011-06-16
  • 打赏
  • 举报
回复
CTime tm;
m_datebegin.GetTime(tm);
CString strbegin=tm.Format("%Y-%m-%d");
CTime t;
m_dateover.GetTime(t);
CString strover=t.Format("%Y-%m-%d");
sprintf(sql, "SELECT* from biao where Date between '"+ strbegin +"'and '"+ strover +"';"); //转换成sql语句
r = sqlite3_exec(db, sql, print_result, &m, &errMsg);
以上是我略改了下的代码,此时系统报错:
error C2678: 二进制“+”: 没有找到接受“const char [190]”类型的左操作数的运算符(或没有可接受的转换)
lingshawu 2011-06-16
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 lisunlin0 的回复:]
相关sql语句可以先在sqlite命令行里面做验证,验证通过了,再使用自己的编程语言"凑"出来.
sqlite没有原生的时间/日期支持,在其内部是将日期存储其它类型的.

SQLite does not have a storage class set aside for storing dates and/or times. Instead, the built-in Date And T……
[/Quote]
在sqlite命令行里面做验证,验证过了可实现,用的是具体日期能实现。但就是在C++中的SQL语句转换处,(日期是根据控件选择)转换不到日期显示的样子,无法进行查看。
lingshawu 2011-06-16
  • 打赏
  • 举报
回复
error C2678: 二进制“+”: 没有找到接受“const char [190]”类型的左操作数的运算符(或没有可接受的转换)
还是不行。。。报错
sunlin7 2011-06-16
  • 打赏
  • 举报
回复
相关sql语句可以先在sqlite命令行里面做验证,验证通过了,再使用自己的编程语言"凑"出来.
sqlite没有原生的时间/日期支持,在其内部是将日期存储其它类型的.

SQLite does not have a storage class set aside for storing dates and/or times. Instead, the built-in Date And Time Functions of SQLite are capable of storing dates and times as TEXT, REAL, or INTEGER values:

* TEXT as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS").
* REAL as Julian day numbers, the number of days since noon in Greenwich on November 24, 4714 B.C. according to the proleptic Gregorian calendar.
* INTEGER as Unix Time, the number of seconds since 1970-01-01 00:00:00 UTC.

所以楼主需要自己根据实际情况选择TEXT/REAL/INT中的一种来存储自己的日期,将自己的查询条件做必要转换后再查询.
karlson0211 2011-06-16
  • 打赏
  • 举报
回复
如果用控件类型,你用GETTIM()获得时间然后将其转换成字符串就可以了
CTime Time;
CString szDate1,szDate2;
m_ctlt3.GetTime(Time);
szDate1 = Time.Format("%Y-%m-%d");
m_ctlt4.GetTime(Time);
szDate2 = Time.Format("%Y-%m-%d");
karlson0211 2011-06-16
  • 打赏
  • 举报
回复
你用的是控件类型,我用的是简单值类型
lingshawu 2011-06-16
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 karlson0211 的回复:]
下面代码你看看:
UpdateData(true);
CString strd1,strd2;

strd1.Format("%d-%d-%d",m_t1.GetYear(),m_t1.GetMonth(),m_t1.GetDay());
strd2.Format("%d-%d-%d",m_t2.GetYear(),m_t2.GetMonth(),m_t2.GetDay());
CSt……
[/Quote]
error C2039: “GetYear”: 不是“CDateTimeCtrl”的成员
rror C2039: “GetMonth”: 不是“CDateTimeCtrl”的成员
error C2039: “GetDay”: 不是“CDateTimeCtrl”的成员
麻烦您再帮我看看
zyq5945 2011-06-15
  • 打赏
  • 举报
回复
使用字符串就可以了,把它放在单引号之间。

4,011

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 数据库
社区管理员
  • 数据库
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧