当前位置: 首页 > news >正文

注册登录页面项目

关系型数据库地址:C:\Users\ASUS\AppData\Local\Temp\HuaweiDevEcoStudioDatabases\rdb

#注册页面register.ets

import dataRdb from '@ohos.data.rdb'const STORE_CONFIG = {name: 'weather4.db'
}
const TABLE_NAME = 'weather_info'
const SQL_CREATE_TABLE = `CREATE TABLE IF NOT EXISTS ${TABLE_NAME} (id INTEGER PRIMARY KEY AUTOINCREMENT,username TEXT NOT NULL,password TEXT NOT NULL)
`
async function createTable(context) {try {const store = await dataRdb.getRdbStore(context, STORE_CONFIG, 1)await store.executeSql(SQL_CREATE_TABLE)console.info(`✅ 表 ${TABLE_NAME} 创建成功`)} catch (err) {console.error(`❌ 创建表失败: ${JSON.stringify(err)}`)console.error('错误详情:', err)}
}@Entry
@Component
struct WeatherInsertPage {@State username: string = ''@State password: string = ''async insertuser() {const context = getContext(this)createTable(context)const rdbStore = await dataRdb.getRdbStore(context, { name: 'weather4.db' }, 1)const valueBucket: dataRdb.ValuesBucket = {username: this.username,password: this.password,}try {let rowId = await rdbStore.insert('weather_info', valueBucket)console.info(`✅ 插入成功,行ID: ${rowId}`)} catch (err) {console.error(`❌ 插入失败: ${JSON.stringify(err)}`)}}build() {Column() {Text('用户注册').fontSize(26).fontWeight(FontWeight.Bold).margin({ bottom: 20 })TextInput({ placeholder: '请输入用户名', text: this.username }).onChange(value => this.username = value).margin(10)TextInput({ placeholder: '请输入密码', text: this.password }).onChange(value => this.password = value).type(InputType.Password).margin(10)Button('注册').margin(10).onClick(() => this.insertuser())}.padding(20)}
}

#登录页面项目login.ets

import dataRdb from '@ohos.data.rdb'const STORE_CONFIG = {name: 'weather4.db'
}
const TABLE_NAME = 'weather_info'// 建表语句
const SQL_CREATE_TABLE = `CREATE TABLE IF NOT EXISTS ${TABLE_NAME} (id INTEGER PRIMARY KEY AUTOINCREMENT,username TEXT NOT NULL,password TEXT NOT NULL)
`// 创建数据表
async function createTable(context) {const store = await dataRdb.getRdbStore(context, STORE_CONFIG, 1)await store.executeSql(SQL_CREATE_TABLE)
}// 查询用户是否存在
async function checkLogin(context, username: string, password: string): Promise<boolean> {const store = await dataRdb.getRdbStore(context, STORE_CONFIG, 1)const predicates = new dataRdb.RdbPredicates(TABLE_NAME).equalTo('username', username).equalTo('password', password)const resultSet = await store.query(predicates, ['id'])const hasRow = resultSet.goToFirstRow()resultSet.close()return hasRow
}// UI 页面
@Entry
@Component
struct LoginPage {@State username: string = ''@State password: string = ''@State message: string = ''async aboutToAppear() {const context = getContext(this)await createTable(context) // 页面加载时确保表存在}async login() {const context = getContext(this)const success = await checkLogin(context, this.username, this.password)if (success) {this.message = '✅ 登录成功'} else {this.message = '❌ 用户名或密码错误'}}build() {Column() {Text('用户登录').fontSize(26).fontWeight(FontWeight.Bold).margin({ bottom: 20 })TextInput({ placeholder: '请输入用户名', text: this.username }).onChange(value => this.username = value).margin(10)TextInput({ placeholder: '请输入密码', text: this.password }).onChange(value => this.password = value).type(InputType.Password).margin(10)Button('登录').margin(10).onClick(() => this.login())Text(this.message).fontSize(16).margin(10).fontColor(Color.Red)}.padding(20)}
}

运行结果:

注册页面:

登录页面

http://www.xdnf.cn/news/248185.html

相关文章:

  • dify+ollama+知识库 部署
  • 数字智慧方案6156丨智慧医联体信息化解决方案(50页PPT)(文末有下载方式)
  • 今天的python练习题
  • Spring AOP---面向切面编程由认识到使用
  • pycharm安装的插件怎么显示在右侧
  • 【无标题】四色拓扑收缩模型中环形套嵌结构的颜色保真确定方法
  • 【信息系统项目管理师-论文真题】2024上半年(第一批)论文详解(包括解题思路和写作要点)
  • C++11新特性_自动类型推导_decltype
  • Java内存对象实现聚合查询
  • Unity SpriteMask(精灵遮罩)
  • PMP-第八章 项目质量管理
  • 攻防世界 dice_game
  • 多智能体空域协同中的伦理博弈与系统调停
  • LegalOne:本土与国际视野融合的法律评级,大湾区律师及律师事务所榜单申报启动
  • 【统计方法】方差分析(ANOVA):判断数据差异的统计方法
  • 【Linux】环境基础开发工具使用
  • 26.电流信号的强抗干扰能力运用
  • 深圳第三方软件测试机构如何填补企业空缺并助力市场发展?
  • LintCode第652题-递归版
  • Linux基础指令【下】
  • Leetcode刷题报告2——双指针法
  • 基于DrissionPage的高效爬虫开发:以小说网站数据抓取为例
  • vue自定义表头内容excel表格导出
  • LangChain4j +DeepSeek大模型应用开发——7 项目实战 创建硅谷小鹿
  • SpringAI使用OpenAI API格式调用DeepSeek服务
  • 《AIStarter安装部署全攻略:AI绘画/数字人项目快速上手指南(含Windows环境配置要点)》
  • *(解引用运算符)与 ++(自增运算符)的优先级
  • 开始一个vue项目
  • 《排序算法总结》
  • 60常用控件_QSpinBox的使用