You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Expo安卓端SQLite报错:no such table: cars

Expo安卓端SQLite报错:no such table: cars

我之前也踩过Expo安卓端SQLite的这个坑——iOS一切正常,安卓不管是模拟器、Expo Go还是物理机都报表不存在,大概率是数据库初始化时机不对或者安卓端缓存了旧数据库文件导致的,给你几个实用的解决思路:

1. 确保数据库初始化完成后再执行查询操作

你的initializeDatabase是回调式的异步操作,但useEffect里只是调用了它,没有等待初始化完成就可能开始查询表了。可以用状态变量跟踪数据库是否准备好,等初始化完成后再渲染业务页面:

import React, { useEffect, useState } from 'react';
// ...其他导入保持不变

const App = () => {
  const [isDbReady, setIsDbReady] = useState(false);

  // 把初始化函数改成Promise包裹的异步函数
  const initializeDatabase = async () => {
    return new Promise((resolve, reject) => {
      const db = SQLite.openDatabase('MyCars.db');
      db.transaction((tx) => {
        tx.executeSql(
          `CREATE TABLE IF NOT EXISTS cars (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            brand TEXT,
            model TEXT,
            licensePlate TEXT,
            vin TEXT,
            technicalInspectionDate TEXT,
            serviceInspectionDate TEXT,
            highwayStampValidityDate TEXT,
            techInspNotif1 TEXT,
            techInspNotif2 TEXT,
            servInspNotif1 TEXT,
            servInspNotif2 TEXT,
            highwayStampNotif1 TEXT,
            highwayStampNotif2 TEXT
          );`,
          [],
          () => {
            console.log('Table cars created successfully');
            resolve();
          },
          (error) => {
            console.log('Error creating table: ' + JSON.stringify(error));
            reject(error);
          }
        );
      });
    });
  };

  useEffect(() => {
    const initApp = async () => {
      try {
        await initializeDatabase();
        await registerForPushNotificationsAsync();
        setIsDbReady(true);
      } catch (err) {
        console.error('App initialization failed:', err);
      }
    };
    initApp();
  }, []);

  // ...registerForPushNotificationsAsync函数保持不变

  // 数据库未准备好时显示加载状态
  if (!isDbReady) {
    return <Text>Loading...</Text>; // 可以换成更友好的加载组件
  }

  return (
    <NavigationContainer>
      <Stack.Navigator initialRouteName='Home'>
        <Stack.Screen name='Home' component={HomeScreen} />
        <Stack.Screen name='AddCar' component={AddCarScreen} />
        <Stack.Screen name='CarDetail' component={CarDetailScreen} />
        <Stack.Screen name='EditCar' component={EditCarScreen} />
      </Stack.Navigator>
    </NavigationContainer>
  );
};

export default App;

2. 清除安卓端的缓存(关键!)

安卓的Expo Go或者打包后的APK很容易缓存旧的数据库文件,导致新的CREATE TABLE语句根本没执行:

  • 如果用Expo Go:长按Expo Go图标 → 选择「应用信息」→ 「存储」→ 「清除缓存」,然后重新加载你的项目
  • 如果是物理机上的APK:直接卸载旧APK,重新安装打包后的新版本

3. 检查安卓端数据库文件的路径

虽然Expo的SQLite.openDatabase已经帮我们处理了跨平台路径,但可以额外加个日志确认数据库文件是否存在:

import * as FileSystem from 'expo-file-system';

// 在initializeDatabase里添加
console.log('Database path:', FileSystem.documentDirectory + 'SQLite/MyCars.db');
// 可以用FileSystem.existsAsync检查文件是否存在
const dbExists = await FileSystem.existsAsync(FileSystem.documentDirectory + 'SQLite/MyCars.db');
console.log('Database exists:', dbExists);

4. 排查CREATE TABLE语句的语法问题

虽然iOS能正常执行,但安卓的SQLite对语法的兼容性可能更严格,检查一下:

  • 确保所有字段名没有SQLite保留字(你的字段看起来没问题)
  • 语句末尾的分号要加上(你已经加了)
  • 把错误日志改成打印完整的error对象,而不是只打印message,能看到更多细节

备注:内容来源于stack exchange,提问作者Pyyyta

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.17 12:44:33