You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
# Find rows with the first row displaying column names
92
+
db.execute2('SELECT * FROM test.test2_table')do |row|
70
93
p row
71
94
end
72
-
# [['id', 'name'], [3, 'Charlie']],
95
+
# ["id", "name"]
96
+
# ["3", "Charlie"]
97
+
98
+
# Close the database
99
+
db.close()
100
+
101
+
# Use ChDB::Database.open to automatically close the database connection:
102
+
ChDB::Database.open('file:test.db')do |db|
103
+
result= db.execute('SELECT 1')
104
+
p result.to_a# => [["1"]]
105
+
end
106
+
107
+
# Query with specific output formats (CSV, JSON, etc.):
108
+
# See more details at https://clickhouse.com/docs/interfaces/formats.
109
+
ChDB::Database.open(':memory:')do |db|
110
+
csv_data= db.query_with_format('SELECT 1 as a, 2 as b','CSV')
111
+
p csv_data
112
+
# "1,2\n"
113
+
114
+
json_data= db.query_with_format('SELECT 1 as a, 2 as b','JSON')
115
+
p json_data
116
+
end
73
117
```
74
118
75
119
##Thread Safety
76
120
77
-
When using `ChDB::Database.new` to open a session, all read/write operations within that session are thread-safe. However, currently only one active session is allowed per process. Therefore, when you need to open another session, you must first close the previous session.
78
-
79
-
For example, the following code is fine because only the database
80
-
instance is shared among threads:
121
+
When using`ChDB::Database.new` or`ChDB::Database.open` to open a database connection, all read/write operations within that session are thread-safe. However, currently only one active database connection is allowed per process. Therefore, when you need to open another database connection, you must first close the previous connection.
122
+
**Please note that`ChDB::Database.new`,`ChDB::Database.open`, and`ChDB::Database.close` methods themselves are not thread-safe.** If used in multi-threaded environments, external synchronization must be implemented to prevent concurrent calls to these methods, which could lead to undefined behavior.