[ÊÕ£]
ÓÃsqlite Ö´Ðбê×¼ sql Óï·¨
http://www.umgr.com/blog/PostView.aspx?bpId=36294
1. Ö´ÐÐsqlÓï¾ä
int sqlite3_exec(sqlite3*£¬ const char *sql£¬ sqlite3_callbacksql Óï·¨
£¬ void *£¬ char **errmsg );
Õâ¾ÍÊÇÖ´ÐÐÒ»Ìõ sql Óï¾äµÄº¯Êý¡£
µÚ1¸ö²ÎÊý²»ÔÙ˵ÁË£¬ÊÇÇ°Ãæopenº¯ÊýµÃµ½µÄÖ¸Õ롣˵ÁËÊǹؼüÊý¾Ý½á¹¹¡£
µÚ2¸ö²ÎÊýconst char *sql ÊÇÒ»Ìõ sql Óï¾ä£¬ÒÔ\0½áβ¡£
µÚ3¸ö²ÎÊýsqlite3_callback Êǻص÷£¬µ±ÕâÌõÓï¾äÖ´ÐÐÖ®ºó£¬sqlite3»áÈ¥µ÷ÓÃÄãÌṩµÄÕâ¸öº¯Êý¡£
µÚ4¸ö²ÎÊývoid * ÊÇÄãËùÌṩµÄÖ¸Õësql Óï·¨
£¬Äã¿ÉÒÔ´«µÝÈκÎÒ»¸öÖ¸Õë²ÎÊýµ½ÕâÀÕâ¸ö²ÎÊý×îÖջᴫµ½»Øµ÷º¯ÊýÀïÃ棬Õâ¸öÖ¸Õë±È½ÏÖØÒª£¬¿ÉÒÔÓÃÀ´×÷²ÎÊýµÄ´«µÝ¡£Èç¹û²»ÐèÒª´«µÝÖ¸Õë¸ø»Øµ÷º¯Êý£¬¿ÉÒÔÌîNULL¡£µÈÏÂÎÒÃÇÔÙ¿´»Øµ÷º¯ÊýµÄд·¨£¬ÒÔ¼°Õâ¸ö²ÎÊýµÄʹÓá£
µÚ
5 ¸ö²ÎÊýchar ** errmsg ÊÇ´íÎóÐÅÏ¢¡£×¢ÒâÊÇÖ¸ÕëµÄÖ¸Õë¡£sqlite3ÀïÃæÓкܶà¹Ì¶¨µÄ´íÎóÐÅÏ¢¡£Ö´ÐÐ sqlite3_exec
Ö®ºó£¬Ö´ÐÐʧ°Üʱ¿ÉÒÔ²éÔÄÕâ¸öÖ¸Õ루ֱ½Ó
printf(“%s\n”£¬errmsg)£©µÃµ½Ò»´®×Ö·û´®ÐÅÏ¢£¬Õâ´®ÐÅÏ¢¸æËßÄã´íÔÚʲôµØ·½¡£sqlite3_execº¯Êýͨ¹ýÐÞ¸ÄÄã´«ÈëµÄÖ¸ÕëµÄÖ¸
Õ룬°ÑÄãÌṩµÄÖ¸ÕëÖ¸Ïò´íÎóÌáʾÐÅÏ¢£¬ÕâÑùsqlite3_execº¯ÊýÍâÃæ¾Í¿ÉÒÔͨ¹ýÕâ¸ö char*µÃµ½¾ßÌå´íÎóÌáʾ¡£
˵Ã÷£ºÍ¨³££¬
sqlite3_callback ºÍËüºóÃæµÄ void * ÕâÁ½¸öλÖö¼¿ÉÒÔÌî NULL¡£ÌîNULL±íʾÄã²»ÐèÒª»Øµ÷¡£±ÈÈçÄã×ö insert
²Ù×÷£¬×ö delete ²Ù×÷£¬¾ÍûÓбØҪʹÓûص÷¡£¶øµ±Äã×ö select ʱ£¬¾ÍҪʹÓûص÷£¬ÒòΪ sqlite3
°ÑÊý¾Ý²é³öÀ´£¬µÃͨ¹ý»Øµ÷¸æËßÄã²é³öÁËʲôÊý¾Ý¡£
2.exec µÄ»Øµ÷
typedef int (*sqlite3_callback)(void*£¬int£¬char**£¬ char**);
ÄãµÄ»Øµ÷º¯Êý±ØÐ붨Òå³ÉÉÏÃæÕâ¸öº¯ÊýµÄÀàÐÍ¡£ÏÂÃæ¸ø¸ö¼òµ¥µÄÀý×Ó£º
// sqlite ÿ²éµ½Ò»Ìõ¼Ç¼£¬¾Íµ÷ÓÃÒ»´ÎÕâ¸ö»Øµ÷
int LoadMyInfo( void * para£¬ int n_column£¬ char ** column_value£¬ char ** column_name )
{
//paraÊÇÄãÔÚ sqlite3_exec Àï´«ÈëµÄ void * ²ÎÊý
//ͨ¹ýpara²ÎÊý£¬Äã¿ÉÒÔ´«ÈëһЩÌØÊâµÄÖ¸Õ루±ÈÈçÀàÖ¸Õë¡¢½á¹¹Ö¸Õ룩£¬È»ºóÔÚÕâÀïÃæÇ¿ÖÆת»»³É¶ÔÓ¦µÄÀàÐÍ£¨ÕâÀïÃæÊÇvoid*ÀàÐÍ£¬±ØÐëÇ¿ÖÆת»»³ÉÄãµÄÀàÐͲſÉÓã©¡£È»ºó²Ù×÷ÕâЩÊý¾Ý
//n_columnÊÇÕâÒ»Ìõ¼Ç¼ÓжàÉÙ¸ö×ֶΠ(¼´ÕâÌ
Ïà¹ØÎĵµ£º
Ç°Ðò£º... 1
Ò»¡¢ °æ±¾... 1
¶þ¡¢ »ù±¾±àÒë... 2
Èý¡¢ SQLITE²Ù×÷ÈëÃÅ... 2
£¨1£© »ù±¾Á÷³Ì... 2
£¨2£© SQLÓï¾ä²Ù×÷... 4
£¨3£© ²Ù×÷¶þ½øÖÆ... 8
£¨4£© ÊÂÎñ´¦Àí... 10
ËÄ¡¢ ¸øÊý¾Ý¿â¼ÓÃÜ... 10
Îå¡¢ ºó¼Ç... 25
Ç°Ðò£º
Sqlite3 µÄÈ·ºÜºÃÓá£Ð¡ÇÉ¡¢Ëٶȿ졣µ«ÊÇÒòΪ·Ç΢ÈíµÄ²úÆ·£¬°ïÖúÎĵµ×ܾõµÃ²»¹» ......
£¨4£© ÊÂÎñ´¦Àí
sqlite ÊÇÖ§³ÖÊÂÎñ´¦ÀíµÄ¡£Èç¹ûÄãÖªµÀÄãҪͬ²½É¾³ýºÜ¶àÊý¾Ý£¬²»·Â°ÑËüÃÇ×ö³ÉÒ»¸öͳһµÄÊÂÎñ¡£
ͨ³£Ò»´Î sqlite3_exec ¾ÍÊÇÒ»´ÎÊÂÎñ£¬Èç¹ûÄãҪɾ³ý1ÍòÌõÊý¾Ý£¬sqlite¾Í×öÁË1Íò´Î£º¿ªÊ¼ÐÂÊÂÎñ->ɾ³ýÒ»ÌõÊý¾Ý->Ìá½»ÊÂÎ ......
zz from https://www.dream4ever.org/showthread.php?t=86440 feuvan @ DRL
ÔÎÄÔÚhttp://sqlitecn.feuvan.net/faq.html
Óï·¨²¿·ÖÕýÔÚ·ÒëÖУ¬¾´ÇëÆÚ´ý
FAQÖÐÎÄ°æ
ÈçºÎ´´½¨×ÔÔö×ֶΣ¿
SQLite Ö§³ÖÄÄЩÊý¾ÝÀàÐÍ£¿
ΪʲôÄÜÏò SQLite Êý¾Ý¿âµÄÕûÐÍ×Ö¶ÎÖвåÈë×Ö·û´®£¿
Ϊʲô SQLite ÈÏΪ±í´ïʽ '0'=='00' ΪÕ棿
......
Åжϱí´æÔڵķ½·¨ºÜ¼òµ¥,ÍøÉϺܶà:
SELECT COUNT(*) from sqlite_master where type='table' and name='%s'" % tname;
ÄÇôÅжÏ×Ö¶ÎÊÇ·ñ´æÔÚ, »òÕß˵ÈçºÎÅжϱíµÄ°æ±¾ÊÇ·ñ×îоÍÖ»ÐèÒª:
select * from sqlite_master where tbl_name='tblContactList';
sqlite_master µÄ±í½á¹¹ÈçÏÂ:
type |name  ......
ÕâÊÇÒ»¸öÓйطÖÒ³µÄʵÀý,½ö¹©²Î¿¼(´úÂëÀ´×ÔÍøÂç)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SQLite;
using System.Threading;
using System.Collections;
us ......