我有一个用 Python 2.7 编写的项目,其中主程序需要频繁访问 sqlite3 数据库以写入日志、测量结果、获取设置等。
目前我有一个 db 模块,其中包含 add_log()、get_setting() 等函数,其中的每个函数基本上如下所示:
def add_log(logtext):
try:
db = sqlite3.connect(database_location)
except sqlite3.DatabaseError as e:
db.close() # try to gracefully close the db
return("ERROR (ADD_LOG): While opening db: {}".format(e))
try:
with db: # using context manager to automatically commit or roll back changes.
# when using the context manager, the execute function of the db should be used instead of the cursor
db.execute("insert into logs(level, source, log) values (?, ?, ?)", (level, source, logtext)) …Run Code Online (Sandbox Code Playgroud) 在使用 PIC24FJ128GB204 的 MPLAB X 中学习嵌入式 C。
到目前为止,我主要听说在嵌入式设备上应该尽可能多地使用无符号类型(尤其是?),所以我开始使用 uint8_t 数组来保存字符串。但是,如果我从 stdlib.h 调用 itoa,它需要一个指向有符号字符 (int8_t) 数组的指针:
extern char * itoa(char * buf, int val, int base);
当我在无符号数组上使用 itoa 后尝试编译时,这一点特别清楚:
main.c:317:9: warning: pointer targets in passing argument 1 of 'itoa' differ in signedness
c:\program files (x86)\microchip\xc16\v1.36\bin\bin\../..\include/stdlib.h:131:15: note: expected 'char *' but argument is of type 'unsigned char *'
Run Code Online (Sandbox Code Playgroud)
在其他平台上搜索 itoa 的实现,这似乎是常见的情况。
这是为什么?
(我还注意到大多数实现都需要值/指针/基数,而出于某种原因,来自 Microchip 的 stdlib.h 首先需要指针。我花了一段时间才意识到这一点。)