Initial value for sqlite::connection

Hello to all,

fn main() {
	let mut dbstruc = DbStruct {
		conn:		???,   // empty connection initialize
		db_name:	String::from("BilderPool.db"),
		tbl_name:	String::from("bild"), 
//		daten: 		Vec::new(),
	};
	// Connection zur Datenbank aufbauen, ggf. wird die Tabelle erstellt
	f_get_connection(&dbstruc);
}

struct DbStruct {
	conn: 		sqlite::Connection,
	db_name:	String,
	tbl_name:	String,
//	daten:		Vec<String>		// 
}

fn f_get_connection(&f_db: &DbStruct ) {	
    f_db.conn = sqlite::open(f_db.db_name).unwrap();  
    f_create_table(&f_db)   	
}


fn f_create_table(f_dbstruc: &DbStruct) {
	f_dbstruc.conn.execute(format!("CREATE TABLE IF NOT EXISTS {} 
		(
		 bild_id INTEGER PRIMARY KEY AUTOINCREMENT,
		 bild_name TEXT NOT NULL UNIQUE,
		 bild_ort TEXT NOT NULL,
		 bild_status INTEGER
		);", f_dbstruc.tbl_name),).unwrap();
}

Error message:

Compiling rust_sqlite_neu v0.1.0 (/home/guenter/mydaten/entw/rust/rust_sqlite_neu)
error: expected expression, found `?`
 --> src/main.rs:8:10
  |
7 |     let mut dbstruc = DbStruct {
  |                       -------- while parsing this struct
8 |         conn:        ???,   // empty connection initialize
  |                      ^ expected expression

error: aborting due to previous error

error: could not compile `rust_sqlite_neu`

To learn more, run the command again with --verbose.

What do I need to insert for <???> to fix the resulting error?
Is the chosen path reasonable and practicable?

Thanks for your help and tips.
See you then ...
MfG
G√ľnter

have apparently found a solution. The program compiles and also produces the desired result.

Whether this solution is good I do not know, only you can judge.

fn main() {

	let mut dbstruc = DbStruct {
===>	conn:		sqlite::open("BilderPool.db").unwrap(),   // empty connection initialize
					^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
		db_name:	String::from("BilderPool.db"),
		tbl_name:	String::from("bild"), 
//		daten: 		Vec::new(),
	};

	// Connection zur Datenbank aufbauen, ggf. wird die Tabelle erstellt
	f_get_connection(&mut dbstruc); <======
					  ^^^
}

struct DbStruct {
	conn: 		sqlite::Connection,
	db_name:	String,
	tbl_name:	String,
//	daten:		Vec<String>		// 
}

fn f_get_connection(f_db: &mut DbStruct ) {	 <====
						   ^^^
    f_db.conn = sqlite::open(&f_db.db_name).unwrap();
    
    f_create_table(&f_db)   	
}

fn f_create_table(f_dbstruc: &DbStruct) {
	f_dbstruc.conn.execute(format!("CREATE TABLE IF NOT EXISTS {} 
		(
		 bild_id INTEGER PRIMARY KEY AUTOINCREMENT,
		 bild_name TEXT NOT NULL UNIQUE,
		 bild_ort TEXT NOT NULL,
		 bild_status INTEGER
		);", f_dbstruc.tbl_name),).unwrap();
}

In Rust you need to initialize every field of an object at the same time so there is no way to initialize most of the object then pass it to a function which will initialize the rest. This is deliberate and means it is never possible to observe an object in a half-initialized state (a great way to introduce bugs).

Instead, you should create the connection first and then create the DbStruct with your initialized connection.

fn main() {
    let db_name = String::from("BinderPool.db");
    let tbl_name = String::from("bild");
    let conn = f_get_connection(&db_name, &tbl_name);

  let dbstruc = DbStruct { conn, db_name, tbl_name };
}

fn f_get_connection(db_name: &str, tbl_name: &str) -> sqlite::Connection {
  let conn = sqlite::open(db_name).unwrap();
  f_create_table(&conn, &tbl_name);

  conn
}

fn f_create_table(conn: &sqlite::Connection, table_name: &str) { ... }
}

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.