@chekhxv

Как связать TextField, Button с SQLLite на Swift?

У меня есть два файла
Первый - это вьюн контроллер
Второй - AddButton - В котором находится подключенная база данных
Файл - с заготовленной таблицей
Как мне связать Field'ы и кнопки с sql
Вот мой код

ViewContoller -

import SwiftUI

struct ContentView: View {
@State var UserLogin = ""
var body: some View {
ZStack{
Color(.systemPurple)
.edgesIgnoringSafeArea(.all)
ZStack{
Color(.darkGray)
.frame(width: 450, height: 700)
VStack{
Text("Welcome to our app")
.font(.system(size: 32, weight: .medium, design: .default))
.foregroundColor(.white)
.frame(width: 300,
height: 200)
.padding(10)
VStack{
Image(systemName: "person.circle")
.renderingMode(.original)
.resizable()
.foregroundColor(.white)
.frame(width: 150, height: 150)
}
Spacer()
VStack{
TextField("Login", text: $UserLogin)
.font(.system(size: 15, weight: .medium, design: .default))
.textFieldStyle(RoundedBorderTextFieldStyle())
.background(Color.white)
.frame(width:320)
TextField("Login", text: $UserLogin)
.font(.system(size: 15, weight: .medium, design: .default))
.foregroundColor(.white)
.textFieldStyle(RoundedBorderTextFieldStyle())
.background(Color.white)
.frame(width:320)

}
.padding(10)
VStack{
Button("LOG IN"){
print("LOG IN")
}
.frame(width: 250, height: 30)
.background(Color.white)
.padding(5)
Button("SIGN IN"){
print("SIGN IN")
}
.frame(width: 250, height: 30)
.background(Color.white)
}
Spacer()


}
}
}
}
}

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}

struct Login(LogIn(UserLogin: )){
TextField()
}


AddButton

//
// AddButton.swift
// VD50-2-20.BubnovK.A
//
// Created by Кирилл Бубнов on 10/10/22.
//

import Foundation
import SQLite

func LogIn(UserLogin: String){

do{
let path = NSSearchPathForDirectoriesInDomains(
.documentDirectory, .userDomainMask, true
).first!
_=copyDatabaseIfNeeded(sourcePath: Bundle.main.path(forResource: "LoginPassword", ofType: "db")!)
let db = try Connection("\(path)/LoginPassword.db")
let Users = Table("Users")
let Login = Expression("Login")
try db.run(Users.insert(Login <- UserLogin))
}
catch{
print(error)
}

}

func copyDatabaseIfNeeded(sourcePath: String) -> Bool {
let documents = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
let destinationPath = documents + "/LoginPassword"
let exists = FileManager.default.fileExists(atPath: destinationPath)
guard !exists else { return false }
do {
try FileManager.default.copyItem(atPath: sourcePath, toPath: destinationPath)
return true
} catch {
print("error during file copy: \(error)")
return false
}
}
  • Вопрос задан
  • 56 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы