C/C++ SQLite 資料庫教學與範例

本篇 ShengYu 介紹 C/C++ SQLite 資料庫教學與範例,資料庫的操作無非就是 CRUD,CRUD 分別為 Create 新建資料/增加資料、Read 讀取資料/查詢資料、Update 更新資料、Delete 刪除資料,

CRUD 對應到SQL語法會像是這樣,
Create - 新建資料/增加資料,SQL語法:CREATE/INSERT
Read - 讀取資料/查詢資料,SQL語法:SELECT
Update - 更新資料,SQL語法:UPDATE
Delete - 刪除資料,SQL語法:DELETE

以上幾種都會介紹到,接下來介紹的順序會是 CREATE / INSERT / SELECT / UPDATE / DELETE

Read More

Python SQLite 資料庫教學與範例

本篇 ShengYu 介紹 Python SQLite 資料庫教學與範例。

以下 Python SQLite 資料庫範例大概分為以下幾部份,

  • Python SQLite CREATE 用法與範例
  • Python SQLite INSERT 用法與範例
  • Python SQLite SELECT 用法與範例
  • Python SQLite UPDATE 用法與範例
  • Python SQLite DELETE 用法與範例

這邊先介紹 Python SQLite 最基本的建立 DB 資料庫連線與關閉 DB 資料庫。
例如我要建立 tutorial.db 這個 database 連線,

python3-sqlite3-tutorial-open-db.py
1
2
3
4
5
6
7
8
9
10
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sqlite3

con = sqlite3.connect("tutorial.db")
cur = con.cursor()

# ...

con.close()

以上就是最簡單的 Python SQLite 範例,接下來我們來看看 Python SQLite 常見的操作範例。

Read More

Java File 用法與範例

本篇介紹 Java File 用法與範例,Java File 常見用法是用來判斷路徑的檔案是否存在,或者判斷路徑的資料夾是否存在,還可以取得檔案的長度大小、檔案更動日期等等資訊。

Read More

JavaScript onclick event submit form 用法範例

本篇介紹如何在 JavaScript onclick event 事件中用 form.submit() 函式來 submit form 送出表單。

HTML 基本的 submit form 用法

HTML 基本的 submit form 寫法如下,有個 form 標籤,裡面有個 input 標籤 type="submit"

submit-form.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<html>
<head>
<title>Form Submit Example</title>
<!-- Include CSS File Here
<link rel="stylesheet" href="css/xxx.css"/>
-->
<!-- Include JS File Here
<script src="js/xxx.js"></script>
-->
</head>
<body>
<div class="container">
<div class="main">
<h2>Form Submit Example</h2>
<form action="#" method="post" name="form_name" id="form_id" class="form_class" >
<label>Name :</label><input type="text" name="name" id="name" placeholder="Name" />
<label>Email :</label><input type="text" name="email" id="email" placeholder="Valid Email" />
<input type="submit" name="submit_id" id="btn_id" value="Submit"/>
</form>
</div>
</div>
</body>
</html>

Read More

6 種查詢 SQLite 版本的方法

本篇 ShengYu 介紹 6 種查詢 SQLite 版本的方法,分為這幾種方法,

  • sqlite3.h 標頭檔查詢 SQLite 版本
  • C 語言查詢 SQLite 版本
  • Python 查詢 SQLite 版本
  • SQL 語法查詢 SQLite 版本
  • Command 下指令查詢 SQLite 版本
  • 從 database 檔案本身查詢 SQLite 版本

sqlite3.h 標頭檔查詢 SQLite 版本

一種是從 sqlite3.h 標頭檔去看,在 sqlite3.h 搜尋 SQLITE_VERSION 這個定義就可以找到版本號碼了。

sqlite3.h
1
2
3
#define SQLITE_VERSION        "3.40.1"
#define SQLITE_VERSION_NUMBER 3040001
#define SQLITE_SOURCE_ID "2022-12-28 14:03:47 df5c253c0b3dd24916e4ec7cf77d3db5294cc9fd45ae7b9c5e82ad8197f38a24"

C 語言查詢 SQLite 版本

C 語言的話可呼叫 sqlite3_libversion() API, 有時候系統已經有安裝一份 SQLite 的標頭檔了,但是你想用下載的新版的 SQLite,可能會因為你設置不對造成程式跑去連結系統的那一份,這時用呼叫 sqlite3_libversion() API 來看 SQLite 版本就會比較準確知道你是使用哪一份 SQLite,

Read More

C/C++ rand 產生亂數用法與範例

本篇 ShengYu 介紹 C/C++ rand 產生亂數用法與範例。

C/C++ 要使用 rand 的話需要引入的標頭檔 <stdlib.h>,如果要使用 C++ 的標頭檔則是引入 <cstdlib>
rand 函式原型為

1
int rand (void);

rand() 所產生的亂數是一個整數,其值介於 0 到 RAND_MAX 之間,RAND_MAX 的值會是 2147483647,跟 INT_MAX 一樣。

C/C++ rand 用法

rand() 產生 0-9 亂數寫法如下,

1
2
3
4
5
6
7
8
9
10
// g++ cpp-rand.cpp -o a.out
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main() {
int n = rand() % 10; // 產生 0-9 亂數
printf("n = %d\n", n);
return 0;
}

結果如下,會發現執行3次的結果亂數都一樣,

1
2
3
4
5
6
$ ./a.out 
n = 3
$ ./a.out
n = 3
$ ./a.out
n = 3

這次我們以時間做為亂數種子,再試看看會變成怎麼樣?

cpp-rand-2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
// g++ cpp-rand-2.cpp -o a.out
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main() {
srand(time(NULL)); // 設定時間為亂數種子

int n = rand() % 10; // 產生 0-9 亂數
printf("n = %d\n", n);
return 0;
}

Read More

C/C++ Linux shared memory 與 mmap 用法範例

今天 ShengYu 來介紹 Linux 跨行程通訊 IPC 中的其中一種方式:共享記憶體 Shared Memory 以及 named semaphore,這通常也是 OS (operating system)作業系統或linux系統程式課程中的一部分,以下範例分為生產者 producer 與消費者 consumer,基本上會使用到 mmap 與 shm_open 這幾個函式。

共享記憶體傳遞 string 字串的範例

生產者 producer 寫入 string 字串的範例如下,
這邊的 shm_open 是使用 O_CREAT | O_RDWR 能建立且能讀取寫入,同樣的 mmap 也使用 PROT_READ | PROT_WRITE 能讀取跟寫入。

shm-posix-producer-string.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// gcc shm-posix-producer-string.c -o produce -lrt && ./produce
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/types.h>

void display(char *prog, char *bytes, int n) {
printf("display: %s\n", prog);
for (int i = 0; i < n; i++) {
printf("%02x%c", bytes[i], ((i+1)%16) ? ' ' : '\n');
}
printf("\n");
}

int main() {
const int SIZE = 4096;
const char *name = "OS";
const char *message0= "Studying ";
const char *message1= "Operating Systems ";
const char *message2= "Is Fun!";

int shm_fd;
void *ptr;

/* create the shared memory segment */
shm_fd = shm_open(name, O_CREAT | O_RDWR, 0666);

/* configure the size of the shared memory segment */
ftruncate(shm_fd,SIZE);

/* now map the shared memory segment in the address space of the process */
ptr = mmap(0,SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
if (ptr == MAP_FAILED) {
printf("Map failed\n");
return -1;
}

/**
* Now write to the shared memory region.
*
* Note we must increment the value of ptr after each write.
*/
char *str = (char *)ptr;
sprintf(str,"%s",message0);
str += strlen(message0);
sprintf(str,"%s",message1);
str += strlen(message1);
sprintf(str,"%s",message2);
str += strlen(message2);
display("prod", ptr, 64);

return 0;
}

Read More

Windows XAMPP 攜帶版安裝教學

本篇 ShengYu 介紹 Windows XAMPP 攜帶版安裝教學,以往要安裝 Apache + MySQL + PHP + Perl 這些環境就花費許久時間,所以出現了 XAMPP 讓開發者快速地建置後端環境,以前 XAMPP 是 Apache + MySQL + PHP + Perl 的縮寫。MySQL 為目前市佔率最高的資料庫系統。因為當初 MySQL 被甲骨文公司收購後,開發者擔心會有後患,像是轉為商用等等,於是社群就 clone 出 MariaDB,所以現在 XAMPP 上使用的是 MariaDB 而非 MySQL。但是這兩個系統幾乎完全相同。現在 XAMPP 則是 Apache + MariaDB + PHP 的縮寫。

XAMPP 到官網 https://www.apachefriends.org/zh_tw/download.html 或者 sourceforge 下載 xampp portable 隨身攜帶版

把解壓縮後的 xampp 資料夾放在硬碟或USB隨身(硬)碟的最上層,例如: C:\xampp 或 E:\xampp。

Read More