当我尝试将值插入表格时,我的应用程序正在生成错误。
这是我的代码:
import UIKit
class RegisterViewController: UIViewController {
@IBOutlet weak var firstNameTextField: UITextField!
@IBOutlet weak var lastNameTextField: UITextField!
@IBOutlet weak var emailTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!
@IBOutlet weak var confirmPasswordTextField: UITextField!
var databasePath = NSString()
override func viewDidLoad() {
super.viewDidLoad()
let filemgr = NSFileManager.defaultManager()
let dirPaths =
NSSearchPathForDirectoriesInDomains(.DocumentDirectory,
.UserDomainMask, true)
let docsDir = dirPaths[0]
databasePath = (docsDir as NSString).stringByAppendingPathComponent(
"users.db")
if !filemgr.fileExistsAtPath(databasePath as String) {
let userDB = FMDatabase(path: databasePath as String)
if userDB == nil {
print("Error: \(userDB.lastErrorMessage())")
}
if userDB.open() {
let sql_stmt = "CREATE TABLE IF NOT EXISTS USERS (ID INTEGER PRIMARY KEY AUTOINCREMENT, FIRSTNAME VARCHAR(20), LASTNAME VARCHAR(20), EMAIL text, PASSWORD VARCHAR(20))"
if !userDB.executeStatements(sql_stmt) {
print("Error: \(userDB.lastErrorMessage())")
}
userDB.close()
} else {
print("Error: \(userDB.lastErrorMessage())")
}
}
}
@IBAction func registerButtonTapped(sender: UIButton) {
//GetValues
let userFirstName = firstNameTextField.text
let userLastName = lastNameTextField.text
let userEmail = emailTextField.text
let userPassword = passwordTextField.text
let userConfirmPassword = confirmPasswordTextField.text
//check for empty fields
if(userFirstName!.isEmpty || userLastName!.isEmpty || userEmail!.isEmpty || userPassword!.isEmpty || userConfirmPassword!.isEmpty)
{
//Display alert Message
displayMyAlertMessage("All fields are required")
return
}
//check passwords match
if (userPassword != userConfirmPassword)
{
//display alert message
displayMyAlertMessage("Passwords do not match")
return
}
//Store Data
let userDB = FMDatabase(path: databasePath as String)
if userDB.open() {
let insertSQL = "INSERT INTO USERS (firstname, lastname, email, password) VALUES ('\(firstNameTextField.text)', '\(lastNameTextField.text)', '\(emailTextField.text)', '\(passwordTextField.text)')"
let result = userDB.executeUpdate(insertSQL,
withArgumentsInArray: nil)
if !result {
displayMyAlertMessage("Operation failed")
print("Error: \(userDB.lastErrorMessage())")
return
} else {
displayMyAlertMessage("Thank you for registering \(firstNameTextField.text)")
}
}
//display alert message with confirmation
let myAlert = UIAlertController(title:"Alert", message:"Registration Successful", preferredStyle: UIAlertControllerStyle.Alert)
let okAction = UIAlertAction(title:"Ok", style:UIAlertActionStyle.Default) { action in self.dismissViewControllerAnimated(true, completion:nil)
}
myAlert.addAction(okAction)
self.presentViewController(myAlert, animated:true, completion:nil)
}
func displayMyAlertMessage(userMessage:String)
{
let myAlert = UIAlertController(title:"Alert", message:
userMessage, preferredStyle:
UIAlertControllerStyle.Alert);
let okAction = UIAlertAction(title:"Ok", style:
UIAlertActionStyle.Default, handler:nil)
myAlert.addAction(okAction)
self.presentViewController(myAlert, animated:true,
completion:nil)
}
}
错误:没有这样的表:USERS
但是,这是在viewDidLoad()
函数中创建的。我认为不需要调用viewDidLoad()
函数并且应该创建用户表是错误的吗?
答案 0 :(得分:1)
您需要检查应用文档目录中的“users.db”文件。
如果您使用的是模拟器,则会获得以下行中的路径
let dirPaths =
NSSearchPathForDirectoriesInDomains(.DocumentDirectory,
.UserDomainMask, true)
粘贴路径并单击“开始”
或者
如果您使用iPhone之类的设备,那么在iFunbox(here)的帮助下,您可以访问该文件。
如果存在“users.db”文件,则将其复制到桌面并使用sqlitebrowser应用程序(here)打开。
您将能够看到所有表格,列,数据等。 您可以找出错误,以及为什么不插入数据。
注意:不建议其他应用使用此功能。只是为了调试 目的,只有你的应用程序。