Skip to content

Commit

Permalink
try fix: sqlite
Browse files Browse the repository at this point in the history
  • Loading branch information
SALTWOOD committed Aug 28, 2024
1 parent 34b350d commit cf0a481
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions src/sqlite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,20 @@ export class SQLiteHelper {
this.db.exec(createTableSQL);
}

// 插入数据
public insert<T extends object>(obj: T): void {
const tableName = this.getTableName(obj);
const data = obj as Record<string, any>;
const ignoredFields = (obj.constructor as any).ignoredFields || [];
const kvp = Object.keys(data).filter(key => !ignoredFields.includes(key));

const columns = kvp.join(', ');
const placeholders = kvp.map(() => '?').join(', ');
const values = Object.values(data);

const placeholders = kvp.map(() => '?').join(', '); // 添加占位符
const values = kvp.map(key => data[key]); // 取得对应的值
const insertSQL = `INSERT INTO ${tableName} (${columns}) VALUES (${placeholders})`;
const stmt = this.db.prepare(insertSQL);
stmt.run(values);
}
stmt.run(...values); // 使用展开运算符传递值
}

// 查询数据
public select<T>(type: { new (): T }, columns: string[], whereClause?: string, params?: any[]): T[] {
Expand Down

0 comments on commit cf0a481

Please sign in to comment.