当前位置: 首页 > 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)}
}

运行结果:

注册页面:

登录页面

相关文章:

  • Ocelot\Consul\.NetCore的微服务应用案例
  • 构建现代分布式云架构的三大支柱:服务化、Service Mesh 与 Serverless
  • 微软推出数款Phi 4“开放式”人工智能模型
  • 系统架构设计师:设计模式——创建型设计模式
  • 微软与Meta大幅增加人工智能基础设施投入
  • 普通 html 项目也可以支持 scss_sass
  • Anaconda中配置Pyspark的Spark开发环境
  • 【中间件】bthread_数据结构_学习笔记
  • terraform 删除资源前先校验资源是否存在关联资源
  • AJAX 实例
  • 【Linux】线程池和线程补充内容
  • Qwen3 正式发布
  • C++——入门基础(2)
  • 工 厂 模 式
  • 游戏引擎学习第252天:允许编辑调试值
  • 企业内训|智能驾驶与智能座舱技术——某汽车厂商
  • 【Qt】网络
  • Python项目源码69:Excel数据筛选器1.0(tkinter+sqlite3+pandas)
  • 《数据结构初阶》【顺序表/链表 精选15道OJ练习】
  • 【数据结构】- 栈
  • 三大猪企一季度同比均实现扭亏为盈,营收同比均实现增长
  • 上海市十六届人大常委会第二十一次会议表决通过有关人事任免事项
  • 五一“拼假”催热超长假期,热门酒店民宿一房难求
  • 江西省公安厅警务保障部原主任辛卫平主动交代问题,正接受审查调查
  • 上海通报5起违反中央八项规定精神问题
  • 香港警务处高级助理处长叶云龙升任警务处副处长(行动)