Check if a column exists in a given table.
(&self, table_name: &str, col_name: &str)
| 506 | |
| 507 | /// Check if a column exists in a given table. |
| 508 | pub async fn col_exists(&self, table_name: &str, col_name: &str) -> Result<bool> { |
| 509 | let query_only = true; |
| 510 | self.call(query_only, move |conn| { |
| 511 | let mut exists = false; |
| 512 | // `PRAGMA table_info` returns one row per column, |
| 513 | // each row containing 0=cid, 1=name, 2=type, 3=notnull, 4=dflt_value |
| 514 | conn.pragma(None, "table_info", table_name.to_string(), |row| { |
| 515 | let curr_name: String = row.get(1)?; |
| 516 | if col_name == curr_name { |
| 517 | exists = true; |
| 518 | } |
| 519 | Ok(()) |
| 520 | })?; |
| 521 | |
| 522 | Ok(exists) |
| 523 | }) |
| 524 | .await |
| 525 | } |
| 526 | |
| 527 | /// Execute a query which is expected to return zero or one row. |
| 528 | pub async fn query_row_optional<T, F>( |