批处理空串的比较

批处理中会将引号本身识别为字符串的一部分。

 SET a=
 IF %a%=="" (
  ECHO True
 ) ELSE (
  ECHO False
 )

 Output: False

我们不妨在变量外面再封装一对引号,整体比较,便可以解决这个问题。


 SET a=
 IF "%a%"=="" (
  ECHO True
 ) ELSE (
  ECHO False
 )

 Output: True 

我想把dft中的4列全部数据,保存至瀚高数据库中,请帮我编写代码 参考我保存其他数据的模式,参考代码如下: # 数据库操作 conn = create_connection() # 使用公共定义的数据库连接 cursor = conn.cursor() db_config = get_db_config() dbtype = db_config['dbtype'] try: if batch_data: sql = '' # 首先初始化一个空串 if dbtype == 'mysql': sql = """ INSERT INTO station_wrf_info (station_id, station_name, latitude, longitude, height, temperature, relative_humidity, wind_speed, wind_direction, vertical_velocity_pressure, pressure, data_dt) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) ON DUPLICATE KEY UPDATE station_name = VALUES(station_name), latitude = VALUES(latitude), longitude = VALUES(longitude), height = VALUES(height), temperature = VALUES(temperature), relative_humidity = VALUES(relative_humidity), wind_speed = VALUES(wind_speed), wind_direction = VALUES(wind_direction), vertical_velocity_pressure = VALUES(vertical_velocity_pressure), pressure = VALUES(pressure), data_dt = VALUES(data_dt) """ elif dbtype == 'highgo': sql = """ INSERT INTO station_wrf_info (station_id, station_name, latitude, longitude, height, temperature, relative_humidity, wind_speed, wind_direction, vertical_velocity_pressure, pressure, data_dt) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) ON CONFLICT (station_id, data_dt,height) DO nothing """ # 批量插入 batch_size = 1000 for i in range(0, len(batch_data), batch_size): cursor.executemany(sql, batch_data[i:i + batch_size]) conn.commit() logger.info(f"成功处理 {len(batch_data)} 条数据(插入或更新)!") except Exception as e: logger.info(f"Upsert 数据库时出错:{e}") conn.rollback() finally: cursor.close() conn.close()
最新发布
09-30
### 空串的定义及其在编程中的使用 #### 空串的定义 空串是指长度为零的字符串,通常表示为 `""` 或者 `''`。根据引用内容[^1],字符串可以通过结构体来定义,其中包含一个字符数组和一个记录字符串长度的整型变量。对于空串而言,其字符数组为空,且长度变量值为 0。 ```c typedef struct { char ch[MAXSIZE]; int length; } SqString; ``` 当创建一个空串时,可以通过将 `length` 设置为 0 来表示该字符串不包含任何字符[^1]。 #### 空串的用法 在编程中,空串具有以下几种常见的用途: 1. **初始化字符串变量** 在许多编程语言中,字符串变量可以被初始化为空串。例如,在 C++ 中: ```cpp std::string str = ""; // 初始化为空串 ``` 2. **字符串拼接的起点** 空串可以用作字符串拼接操作的起点。例如,在 Python 中: ```python result = "" for word in ["hello", "world"]: result += word + " " print(result.strip()) # 输出:hello world ``` 3. **判断字符串是否为空** 空串常用于判断一个字符串是否为空。例如,在 JavaScript 中: ```javascript let str = ""; if (str === "") { console.log("字符串为空"); } ``` 4. **默认返回值** 在某些函数中,如果未找到符合条件的结果,可以返回空串作为默认值。例如,在 Python 中: ```python def find_substring(s, target): index = s.find(target) return s[index:] if index != -1 else "" ``` 5. **字符串比较** 空串可以参与字符串比较操作。例如,在 Java 中: ```java String str = ""; if (str.equals("")) { System.out.println("字符串为空"); } ``` #### 注意事项 尽管空串在编程中非常常见,但在实际使用时需要注意以下几点: - 空串与 `null` 或 `None` 不同。空串是一个有效的字符串对象,而 `null` 或 `None` 表示没有对象。 - 某些语言中,空串可能被视为假值(如 Python 和 JavaScript),因此在逻辑判断时需要特别注意。 ### 示例代码 以下是一个简单的 C 语言示例,展示如何定义和使用空串: ```c #include <stdio.h> #include <string.h> #define MAXSIZE 255 typedef struct { char ch[MAXSIZE]; int length; } SqString; void init_empty_string(SqString *s) { s->ch[0] = '\0'; // 置为空串 s->length = 0; // 长度为 0 } int main() { SqString s; init_empty_string(&s); printf("字符串是否为空: %d\n", s.length == 0); // 输出 1 表示为空 return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值