我正在向我的应用程序添加一项功能,允许用户包含经常性费用。我将此信息存储在一个表中,该表是使用以下代码创建的:
// Creating or opening a table to store recurring expenses.
try {
db.transaction((tx) => {
tx.executeSql(
"CREATE TABLE IF NOT EXISTS rexpenses (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, amount REAL, category TEXT, startdate DATETIME DEFAULT CURRENT_TIMESTAMP, recurrancedate DATETIME, repeattype STRING);",
[],
() => {
console.log("rexpenses table created successfully.");
},
(error) => {
console.log("Error creating rexpenses table:", error);
}
);
});
} catch (error) {
console.log("Error executing SQL statement in addExpense.js:", error);
}
要将这些费用添加到表中,我使用以下函数:
function setRecurringExpense (expense) {
const recurringInterval = expense.recurringInterval
const date = new Date()
let recurranceDate;
if (recurringInterval === 'Daily') {
recurranceDate = new Date(date.getTime() + 24 * 60 * 60 * 1000);
} else if (recurringInterval === 'Monthly') {
recurranceDate = new Date(date.getTime());
recurranceDate.setMonth(date.getMonth() + 1);
} else {
recurranceDate = new Date(date.getTime());
recurranceDate.setFullYear(date.getFullYear() + 1);
}
try {
db.transaction((tx) => {
tx.executeSql(
"INSERT INTO rexpenses (name, amount, category, recurrancedate, repeattype) VALUES (?, ?, ?, ?, ?)",
[expense.Name, expense.Amount, expense.Category, recurranceDate, recurringInterval],
(_, { rowsAffected, insertId }) => {
if (rowsAffected > 0) {
console.log("Expense record inserted with ID in rexpenses:", { insertId });
}
},
(_, error) => {
console.log("Error inserting rexpense record:", error);
}
);
});
} catch(error) {
console.log('Error adding data: ', error)
}
}
虽然我已检查所有变量均已正确设置,但
tx.executeSql
语句无法正常工作。尽管没有记录任何错误,但它并未将费用插入表中。此外,tx.executeSql 语句之外的任何内容都会被记录,以便正确调用该函数
我尝试从表中选择数据以检查信息是否正确添加。然而,似乎
tx.executeSql
语句没有向表中添加任何内容。我花了两天时间试图找出问题的根源,但尚未找到解决方案。任何建议或帮助将不胜感激。谢谢你。
const TABLE_NAME = 'rexpenses';
export const createTablesSql = async () => {
return await new Promise((res, rej) => {
db.transaction(tx => {
tx.executeSql(
`CREATE TABLE IF NOT EXISTS ${TABLE_NAME} (
id TEXT PRIMARY KEY AUTOINCREMENT,
name TEXT,
amount REAL,
category TEXT,
startdate DATETIME DEFAULT CURRENT_TIMESTAMP,
recurrancedate DATETIME,
repeattype STRING,
);`,
[],
(_, result) => {
// Table created successfully or already exists
console.log(`Table ${TABLE_NAME} created successfully`, result);
},
(_, error) => {
console.log(`Error creating table ${TABLE_NAME}`, error);
rej(false);
},
);
});
res(true);
});
};
export const insertDataIntoSql = async (data) => {
return await new Promise((res, rej) => {
db.transaction(tx => {
tx.executeSql(
`INSERT INTO ${TABLE_NAME} (id, name, amount, category, startdate, recurrancedate, repeattype)
VALUES (?, ?, ?, ?, ?, ?, ?);`,
[
data.id,
data.name,
data.amount,
data.category,
data.startdate,
data.recurrancedate
data.repeattype
],
() => res(true),
(_, error) => {
console.error('Insert error', error);
rej(false);
},
);
});
});
};
// Usage:
await createTablesSql()
const inserted = await insertDataIntoSql({}) // your data here