顯示具有 C language 標籤的文章。 顯示所有文章
顯示具有 C language 標籤的文章。 顯示所有文章

2023年9月22日 星期五

[C++ ]Poco資料寫入資料庫SQLite解析

  Poco::Data::SQLite::Connector::registerConnector();

    // 建立会话

    Session session("SQLite", "sample.db");

    // 如果有名為abc的表存在,删除

    session << "DROP TABLE IF EXISTS abc", now;

    // 創建Table

    session << "CREATE TABLE abc (Name VARCHAR(30), Address VARCHAR, Age INTEGER(3))", now;

     Statement insert(session);

  //塞一筆資料 #略#

for(int i=0; i<=65535; i++)

{

    insert << "INSERT INTO abc VALUES(?, ?, ?)",use(abc.name),use(abc.address),use(abc.age);

   //在這裡每插入一筆資料 必須使用insert.exexute(); 並執行execute方法

    insert.execute();

}
當使用for loop 時 insert必須都在括弧內宣告及處理,execute使用for loop會造成效能低下

65536筆跑完必須耗時10分鐘

有關于SQLite批量寫入資料耗時的問題有很多帖子可以找

我找到這篇Reference : https://blog.darkthread.net/blog/sqlite-insert-slow/


在UnitTest時 為了達到65536筆資料來滿足line coverage必須花不少時間,因此需要介紹以下的批量寫入

在訪問了Bing大神後(Bing也有推薦此篇文章 : Poco插入大量数据到数据库的优化 | xilixili.net),有找到一種寫法 說是批量寫入需要靠"事務"進行優化

其範例程式碼如下 : 

Poco::Data::Session sess("SQLite", "test.db");
sess << "create table if not exists test ("
        " [col_a] NVARCHAR (256) NOT NULL,"
        " [col_b] NVARCHAR (256) NOT NULL)",
    Poco::Data::Keywords::now;
 
sess.begin();  <=============================重點在這兩筆
for (size_t i = 0; i < 65535; i++) {
    char bufa[64] = {0};
    char bufb[64] = {0};
    sprintf(bufa, "test col a %d %d", i, rand());
    sprintf(bufb, "test col b %d %d", i * 2, rand());
    sess << "insert into test (col_a,col_b) values (?,?)",
        Poco::Data::Keywords::use(bufa),
        Poco::Data::Keywords::use(bufb),
        Poco::Data::Keywords::now;
}
sess.commit();  <=============================重點在這兩筆

中間筆數內的詳細資訊可以略過,因個人專案不同而已 當使用for loop進行插入前後必須宣告

sess.begin(); 以及 sess.commit(); 便可快速且大量塞入資料至Poco資料庫SQLite中


2023年8月31日 星期四

[C++]C++筆記

推薦閱讀書單&網路資源

1.C++ Primer 5th

2.C++ Standard Library headers

3.語言技術:C++ Gossip

字串

reference : https://shengyu7697.github.io/std-string-substr/

using std::string;

std::string::substr  取出子字串

範例 : substr(從第幾個字節之後開始,的多少個字截截取)

std::string abc_str = "hello"

abc_str.substr(1); //扣掉第一個印出後面的 ello

abc_str.substr(1,2); //扣掉第一個印出後面兩個 el 

string m("ssx"); == string m = "ssx";

 

>> //input

<< //Output message

endl = println = \n


abcManager::abcManager(Poco::Logger &L):_logger(L)

初始化


資料型別

using namespace std; //引入標準類別庫   可使用string  => std::string aa = "";

using namespace xxx::yyy

也可寫成

using xxx:yyy

nullptr : 指標字面值

指標宣告

1.shared_ptr宣告方法 

reference :https://cplusplus.com/reference/memory/shared_ptr/operator%20bool/

h檔全宣告方式std::shared_ptr<CppObjName> abc = std::shared_ptr<CppObjName>();

h檔半宣告方式std::shared_ptr<CppObjName> abc;

cpp檔 new : abc = std::shared_ptr<CppObjName>();

abc = NULL;



p_ptr* = NULL;


2.auto_ptr宣告方法

reference :https://openhome.cc/Gossip/CppGossip/autoPtr.html

3.h檔宣告

h檔半宣告 : abc* a;

cpp檔 new : a = new abc();

2022年8月24日 星期三

[C語言]Stack堆疊與Heap堆積的差別

https://www.twblogs.net/a/5d1b5c70bd9eee1e5c8311ef


 Stack 與 Heap 皆為C語言中處理底層記憶體的技術


Stack是有順序的疊加


Heap則會找空的疊加


Heap需要考慮到空間釋放的問題 (GC回收機制)

2021年4月6日 星期二

[C語言韌體]微控制器(MCU)開發雜記








C語言在MCU的開發上使用while迴圈去進行大循環
常見會有while(1){...}的用法
有時候也會出現for( ; ; ) {...}的用法 跟while(1)是一樣的


20210506 更新 do{ ... }while(0)的妙用
do                                                                                             
{                                                                                              
        code statement                                                              
} while (0)

今天在看nRF52-DK 的code時看到官方定義的code出現此環節,一般來說,mcu會以上述while(1) 或for( ; ; ) 來進行無限迴圈的應用,以do{...}while(0)的定義並不是要做無窮迴圈應用為何還是這樣使用呢? 本來以為是coding style習慣問題,在參考完下述連結後,有更多此妙用的介紹。

好處是必執行外,不需要多餘的if else壟餘code 也避免goto的危險性








使用Trace32 debug
 
 





















 












 
 
 















因葦
k
ll
lll
 
 
 

2018年11月15日 星期四

[C語言]常用資料型別長度與範圍

參考微軟網站與其他網站資訊 將持續更新
類型名稱
位元組
其他名稱
值的範圍
int
4
signed
–2,147,483,648 2,147,483,647
unsigned int
4
unsigned
0 4,294,967,295
__int8
1
char
–128 127
unsigned __int8
1
unsigned char
0 255
__int16
2
shortshort intsigned short int
–32,768 32,767
unsigned __int16
2
unsigned shortunsigned short int
0 65,535
__int32
4
signedsigned intint
–2,147,483,648 2,147,483,647
unsigned __int32
4
unsignedunsigned int
0 4,294,967,295
__int64
8
long longsigned long long
–9,223,372,036,854,775,808 9,223,372,036,854,775,807
unsigned __int64
8
unsigned long long
0 18,446,744,073,709,551,615
bool
1
false true
char
1
預設為 –128 127
使用 /J 編譯時為 0 255
signed char
1
–128 127
unsigned char
1
0 255
short
2
short intsigned short int
–32,768 32,767
unsigned short
2
unsigned short int
0 65,535
long
4
long intsigned long int
–2,147,483,648 2,147,483,647
unsigned long
4
unsigned long int
0 4,294,967,295
long long
8
(但是相當於 __int64)
–9,223,372,036,854,775,808 9,223,372,036,854,775,807
unsigned long long
8
(但是相當於 unsigned __int64)
0 18,446,744,073,709,551,615
float
4
3.4E +/- 38 (7 位數)
double
8
1.7E +/- 308 (15 位數)
長雙精度
double 相同
double 相同
extend ascii
1
 
0 255
uint8_t
1
unsigned char
0 255
uint16_t
2
unsigned short int
0 65535
uint32_t
4
unsigned int
0 4294967295