android.database.sqlite.SQLiteException:表contact_data没有名为timestamp_的列

如何解决android.database.sqlite.SQLiteException:表contact_data没有名为timestamp_的列

| 好吧,我想做的是在第一次安装时对我的应用程序进行设置。因此,在第一次访问中,我的应用程序具有访问CallLog,检索所有数据并将其插入应用程序数据库的服务。 我有一个列“ 0”,我想插入呼叫的时间戳。但是,当我尝试插入时,它说该表没有名为timestamp_的列,并且该列的名称明确地写在sql字符串中。我不知道为什么要这么做。 我的CREATE TABLE字符串写在ѭ1中,我访问该字符串并将其拆分成每一行。 这是服务的代码,SQL字符串,然后是DataHandlerDB的代码,后面是创建数据库的方法。 我的服务:
package com.myapp.test;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.rogercg.phonestatistics.CallDataHelper.OpenHelper;

import android.app.Service;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.ContentObserver;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.provider.Contacts;
import android.text.format.DateFormat;
import android.util.Log;
import android.widget.Toast;

public class RatedCallsService extends Service {

    private static final String LOG_TAG = \"RatedCallsService\";
    private Handler handler = new Handler();
    private SQLiteDatabase db;
    private OpenHelper helper;
    private String theDate;
    private String theMonth_;
    private String theYear_;
    private String theDay_;
    public static boolean servReg = false;

    class RatedCallsContentObserver extends ContentObserver {

        public RatedCallsContentObserver(Handler h) {

            super(h);
            helper = new OpenHelper(getApplicationContext());
            db = helper.getWritableDatabase();
            Log.i(LOG_TAG,\"constructor\");

        }

        @Override
        public boolean deliverSelfNotifications() {
            Log.i(LOG_TAG,\"deliver self\");
            return true;

        }

        @Override
        public void onChange(boolean selfChange) {
            super.onChange(selfChange);
            Log.i(LOG_TAG,\"selfchange \" + selfChange);
            searchInsert();

        }
    }

    @Override
    public void onCreate() {

        servReg = true;
        helper = new OpenHelper(getApplicationContext());
        db = DataHandlerDB.createDB(this);

        registerContentObserver();

        Cursor dbsize = DataHandlerDB.selectTopCalls(this
                .getApplicationContext());
        if (dbsize.getCount() == 0) {

            Toast.makeText(this.getApplicationContext(),\"Updating Database.\",Toast.LENGTH_LONG).show();
            Log.i(LOG_TAG,\"Atualizou \" + dbsize.getCount());
            updateDB();

        }
        Cursor currsize = DataHandlerDB.selectTopCalls(this
                .getApplicationContext());
        currsize.moveToFirst();
        Log.i(LOG_TAG,\"cursize \" + currsize.getCount());
    }

    @Override
    public void onDestroy() {

        super.onDestroy();
        db.close();
        this.getApplicationContext()
                .getContentResolver()
                .unregisterContentObserver(
                        new RatedCallsContentObserver(handler));
        servReg = false;
    }

    @Override
    public IBinder onBind(Intent arg0) {

        return null;

    }

    protected void searchInsert() {

        Cursor cursor = getContentResolver().query(
                android.provider.CallLog.Calls.CONTENT_URI,null,android.provider.CallLog.Calls.DATE + \" DESC \");

        if (cursor.moveToFirst()) {

            int numberColumnId = cursor
                    .getColumnIndex(android.provider.CallLog.Calls.NUMBER);
            int durationId = cursor
                    .getColumnIndex(android.provider.CallLog.Calls.DURATION);
            int contactNameId = cursor
                    .getColumnIndex(android.provider.CallLog.Calls.CACHED_NAME);
            int numTypeId = cursor
                    .getColumnIndex(android.provider.CallLog.Calls.CACHED_NUMBER_TYPE);
            int callTypeId = cursor
                    .getColumnIndex(android.provider.CallLog.Calls.TYPE);
            int dateColumnId = cursor
                    .getColumnIndex(android.provider.CallLog.Calls.DATE);

            Date dt = new Date();
            int hours = dt.getHours();
            int minutes = dt.getMinutes();
            int seconds = dt.getSeconds();
            String currTime = hours + \":\" + minutes + \":\" + seconds;

            SimpleDateFormat dateFormat = new SimpleDateFormat(\"M/d/yyyy\");

            Date date = new Date();

            cursor.moveToFirst();

            String contactNumber = cursor.getString(numberColumnId);
            String contactName = (null == cursor.getString(contactNameId) ? \"\"
                    : cursor.getString(contactNameId));
            String duration = cursor.getString(durationId);
            String numType = cursor.getString(numTypeId);
            String callType = cursor.getString(callTypeId);
            String dateColumn = cursor.getString(dateColumnId);

            seconds = Integer.parseInt(duration);

            theDate = dateFormat.format(date);

            if (theDate.length() == 9) {

                theMonth_ = theDate.substring(0,1);
                theDay_ = theDate.substring(2,4);
                theYear_ = theDate.substring(5,9);

            } else if (theDate.length() == 10) {

                theMonth_ = theDate.substring(0,2);
                theDay_ = theDate.substring(3,4);
                theYear_ = theDate.substring(6,10);

            } else if (theDate.length() == 8) {

                theMonth_ = theDate.substring(0,3);
                theYear_ = theDate.substring(4,8);

            }

            ContentValues values = new ContentValues();
            ContentValues values2 = new ContentValues();

            values.put(\"contact_id\",1);
            values.put(\"contact_name\",contactName);
            values.put(\"number_type\",numType);
            values.put(\"contact_number\",contactNumber);
            values.put(\"duration\",Utilities.convertTime(seconds));
            values.put(\"date\",dateFormat.format(date));
            values.put(\"timestamp_\",dateColumn);
            values.put(\"current_time\",currTime);
            values.put(\"cont\",1);
            values.put(\"type\",callType);

            values2.put(\"month\",Utilities.monthName(Integer.parseInt(theMonth_)));
            values2.put(\"duration\",Utilities.convertTime(seconds));
            values2.put(\"year\",theYear_);
            values2.put(\"month_num\",Integer.parseInt(theMonth_));

            if (!db.isOpen()) {
                db = getApplicationContext()
                        .openOrCreateDatabase(
                                \"/data/data/com.myapp.test/databases/calls.db\",SQLiteDatabase.OPEN_READWRITE,null);
            }
            if (duration != \"\") {
                if (Integer.parseInt(duration) != 0) {

                    String existingMonthDuration = DataHandlerDB
                            .selectMonthsDuration(theMonth_,theYear_,this);
                    Integer newMonthDuration;

                    // Verifica se ja existe mes no MONTHS_DUR
                    if (existingMonthDuration != \"\") {

                        newMonthDuration = Integer
                                .parseInt(existingMonthDuration)
                                + Integer.parseInt(duration);

                        values2.put(\"duration\",Utilities.convertTime(newMonthDuration));

                        db.update(DataHandlerDB.MONTHS_DUR,values2,\"year = ?\",new String[] { theYear_ });

                    } else {

                        db.insert(DataHandlerDB.MONTHS_DUR,values2);

                    }

                    Cursor c = DataHandlerDB.selectTimeStamp(this
                            .getApplicationContext());
                    if (c.moveToFirst()) {
                        Log.i(LOG_TAG,\"c.getstr8: \" + c.getString(8));
                        if (!c.getString(8).equals(dateColumn)) {
                            Log.i(LOG_TAG,\"Antes do db.insert line 202\");
                            db.insert(DataHandlerDB.CONTACT_DATA,values);
                        }
                    }
                }
            }
            cursor.close();
        }
    }

    protected void updateDB() {

        Cursor cursor = getContentResolver().query(
                android.provider.CallLog.Calls.CONTENT_URI,null);

        cursor.moveToFirst();

            do {

                int numberColumnId = cursor
                        .getColumnIndex(android.provider.CallLog.Calls.NUMBER);
                int durationId = cursor
                        .getColumnIndex(android.provider.CallLog.Calls.DURATION);
                int contactNameId = cursor
                        .getColumnIndex(android.provider.CallLog.Calls.CACHED_NAME);
                int numTypeId = cursor
                        .getColumnIndex(android.provider.CallLog.Calls.CACHED_NUMBER_TYPE);
                int callTypeId = cursor
                        .getColumnIndex(android.provider.CallLog.Calls.TYPE);
                int dateColumnId = cursor
                        .getColumnIndex(android.provider.CallLog.Calls.DATE);

                Date dt = new Date();
                int hours = dt.getHours();
                int minutes = dt.getMinutes();
                int seconds = dt.getSeconds();
                String currTime = hours + \":\" + minutes + \":\" + seconds;

                SimpleDateFormat dateFormat = new SimpleDateFormat(\"M/d/yyyy\");

                Date date = new Date();

                String contactNumber = cursor.getString(numberColumnId);
                String contactName = (null == cursor.getString(contactNameId) ? \"\"
                        : cursor.getString(contactNameId));
                String duration = cursor.getString(durationId);
                String numType = cursor.getString(numTypeId);
                String callType = cursor.getString(callTypeId);
                String dateColumn = cursor.getString(dateColumnId);

                seconds = Integer.parseInt(duration);

                theDate = dateFormat.format(date);

                if (theDate.length() == 9) {

                    theMonth_ = theDate.substring(0,1);
                    theDay_ = theDate.substring(2,4);
                    theYear_ = theDate.substring(5,9);

                } else if (theDate.length() == 10) {

                    theMonth_ = theDate.substring(0,2);
                    theDay_ = theDate.substring(3,4);
                    theYear_ = theDate.substring(6,10);

                } else if (theDate.length() == 8) {

                    theMonth_ = theDate.substring(0,3);
                    theYear_ = theDate.substring(4,8);

                }

                ContentValues values = new ContentValues();
                ContentValues values2 = new ContentValues();

                values.put(\"contact_id\",1);
                values.put(\"contact_name\",contactName);
                values.put(\"number_type\",numType);
                values.put(\"contact_number\",contactNumber);
                values.put(\"duration\",Utilities.convertTime(seconds));
                values.put(\"date\",dateFormat.format(date));
                values.put(\"timestamp_\",dateColumn);
                values.put(\"current_time\",currTime);
                values.put(\"cont\",1);
                values.put(\"type\",callType);

                values2.put(\"month\",Utilities.monthName(Integer.parseInt(theMonth_)));
                values2.put(\"duration\",Utilities.convertTime(seconds));
                values2.put(\"year\",theYear_);
                values2.put(\"month_num\",Integer.parseInt(theMonth_));

                if (!db.isOpen()) {
                    db = getApplicationContext()
                            .openOrCreateDatabase(
                                    \"/data/data/com.myapp.test/databases/calls.db\",null);
                }
                if (duration != \"\") {
                    if (Integer.parseInt(duration) != 0) {

                        String existingMonthDuration = DataHandlerDB
                                .selectMonthsDuration(theMonth_,this);
                        Integer newMonthDuration;

                        // Verifica se ja existe mes no MONTHS_DUR
                        if (existingMonthDuration != \"\") {

                            newMonthDuration = Integer
                                    .parseInt(existingMonthDuration)
                                    + Integer.parseInt(duration);

                            values2.put(\"duration\",Utilities.convertTime(newMonthDuration));

                            db.update(DataHandlerDB.MONTHS_DUR,new String[] { theYear_ });

                        } else {

                            db.insert(DataHandlerDB.MONTHS_DUR,values2);

                        }
                        db.insert(DataHandlerDB.CONTACT_DATA,values);
                    }
                }

            } while (cursor.moveToNext());      
        cursor.close();
    }

    public void registerContentObserver() {

        Log.i(LOG_TAG,\"Registrou ContentObserver\");
        this.getApplicationContext()
                .getContentResolver()
                .registerContentObserver(
                        android.provider.CallLog.Calls.CONTENT_URI,true,new RatedCallsContentObserver(handler));
    }
}
我的DataHandlerDB。(与数据库一起处理):
public class DataHandlerDB {

private static final String DATABASE_NAME = \"calls.db\";
private static final int DATABASE_VERSION = 1;
protected static final String RATED_CONTACTS = \"rated_contacts\";
protected static final String CONTACT_DATA = \"contact_data\";
protected static final String MONTHS_DUR = \"months_dur\";
private static final String LOG_TAG = \"DataHandlerDB\";

protected static String CONTACT__ID_COL = \"_id\";
protected static String CONTACT_NAME_COL = \"contact_name\";
protected static String CONTACT_NUMBER_COL = \"contact_number\";
protected static String CONTACT_DURATION_COL = \"duration\";
protected static String CONTACT_DATE_COL = \"date\";
protected static String CONTACT_MONTH_COL = \"month\";

// create the DB
public static SQLiteDatabase createDB(Context ctx) {
    OpenHelper helper = new OpenHelper(ctx);
    SQLiteDatabase db = helper.getWritableDatabase();
    helper.onCreate(db);
    helper.onOpen(db);
    db.close();
    return db;
}

public static class OpenHelper extends SQLiteOpenHelper {

    private final Context mContext;

    OpenHelper(Context context) {

        super(context,DATABASE_NAME,DATABASE_VERSION);
        this.mContext = context;

    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        String[] sql = mContext.getString(
                R.string.My_OnCreate).split(\"\\n\");          
        db.beginTransaction();

        try {
            Log.i(LOG_TAG,\"entrou no try\");
            execMultipleSQL(db,sql);
            db.setTransactionSuccessful();                              

        } catch (SQLException e) {

            Log.e(\"Error creating tables and debug data \",e.toString());
            throw e;

        } finally {

            db.endTransaction();

        }
    }

    private void execMultipleSQL(SQLiteDatabase db,String[] sql) {

        for (String s : sql) {

            if (s.trim().length() > 0) {

                db.execSQL(s);
                Log.i(LOG_TAG,\"Str sql \" + s);
            }
        }

    }

    @Override
    public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion) {
        /*
         * Log.w(\"RatedContacts Database\",* \"Upgrading database,this will drop tables and recreate.\");
         * db.execSQL(\"DROP TABLE IF EXISTS \" + RATED_CONTACTS); onCreate(db);
         */
    }

    @Override
    public void onOpen(SQLiteDatabase db) {

        super.onOpen(db);
    }

}

}
SQL字符串(在strings.xml中声明):
<string name=\"My_OnCreate\">
    \"CREATE TABLE IF NOT EXISTS contact_data ( _id INTEGER PRIMARY KEY AUTOINCREMENT,contact_id INTEGER,contact_name VARCHAR(50),number_type VARCHAR(50),contact_number VARCHAR(50),duration TIME,duration_sum TIME,date DATE,timestamp_ VARCHAR(50),current_time TIME,cont INTEGER,type VARCHAR(50),month VARCHAR(50),day VARCHAR(50),year VARCHAR(50) );
    CREATE TABLE IF NOT EXISTS rated_contacts ( _id INTEGER PRIMARY KEY AUTOINCREMENT,year VARCHAR(50) );
    CREATE TABLE IF NOT EXISTS months_dur ( _id INTEGER PRIMARY KEY AUTOINCREMENT,year VARCHAR(250),month_num INTEGER );\"
    </string>  
这是LogCat的错误:   06-13 18:58:05.041:   INFO /数据库(2121):sqlite返回:   错误代码= 1,味精=表   contact_data没有名为的列   时间戳_      06-13 18:58:05.081:   错误/数据库(2121):插入错误   contact_number = 1234545   持续时间= 00:00:05 contact_id = 1   timestamp_ = 1307928304514 number_type = 0   contact_name =当前时间= 18:58:4   type = 2 cont = 1 date = 6/13/2011 06-13   18:58:05.081:      错误/数据库(2121):   android.database.sqlite.SQLiteException:   表contact_data没有名为的列   timestamp_:,而在编译时:INSERT   INTO contact_data(contact_number,   持续时间,contact_id,timestamp_,   number_type,contact_name,   current_time,类型,续,日期)   值(?,?,?,?,?,?,?,?,?,?);     

解决方法

        我认为这是Java和SQLite之间的绑定代码中的错误。您可以尝试将\“ timestamp_ \”重命名为\“ time_stamp \”进行测试吗?     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res