java - android.database.sqlite.SQLiteException : table contact_data has no column named timestamp_

标签 java android sqlite jdbc

嗯,我想做的是在第一次安装时更新我的​​应用程序。因此,在第一次访问时,我的应用程序有一个访问 CallLog 的服务,并检索所有数据并插入应用程序数据库。

我有一列timestamp_,我想插入调用的时间戳。但是当我尝试插入时,它说该表没有名为 timestamp_ 的列,并且在 sql 字符串中明确写入了该列的名称。我不知道为什么要这样做。

我的 CREATE TABLE 字符串写在 strings.xml 中,我访问该字符串并将其拆分为每一行。

这里是Service的代码,SQL String,后面是DataHandlerDB的代码,就是创建DB的方法。

我的服务:

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, null, 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, 1);
                theDay_ = theDate.substring(2, 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, null, 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, null, values);
                        }
                    }
                }
            }
            cursor.close();
        }
    }

    protected void updateDB() {

        Cursor cursor = getContentResolver().query(
                android.provider.CallLog.Calls.CONTENT_URI, null, null, null,
                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, 1);
                    theDay_ = theDate.substring(2, 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, null, values2);

                        }
                        db.insert(DataHandlerDB.CONTACT_DATA, null, 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));
    }
}

My 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, null, 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, 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 months_dur ( _id INTEGER PRIMARY KEY AUTOINCREMENT, month VARCHAR(50), duration TIME, year VARCHAR(250), month_num INTEGER );"
    </string>  

这是来自 LogCat 的错误:

06-13 18:58:05.041: INFO/Database(2121): sqlite returned: error code = 1, msg = table contact_data has no column named timestamp_

06-13 18:58:05.081: ERROR/Database(2121): Error inserting contact_number=1234545 duration=00:00:05 contact_id=1 timestamp_=1307928304514 number_type=0 contact_name= current_time=18:58:4 type=2 cont=1 date=6/13/2011 06-13 18:58:05.081:

ERROR/Database(2121): android.database.sqlite.SQLiteException: table contact_data has no column named timestamp_: , while compiling: INSERT INTO contact_data(contact_number, duration, contact_id, timestamp_, number_type, contact_name, current_time, type, cont, date) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?);

最佳答案

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

关于java - android.database.sqlite.SQLiteException : table contact_data has no column named timestamp_,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6337170/

相关文章:

java - 确定正方形和矩形之间关系的算法

java - 计算代表已填充属性数量的 Java 对象的百分比

Android - 如何使用 DDMS 将文件夹从文件系统复制到 SD 卡?

linux - 将 SQLite 数据库复制到另一个路径

database - 如何使用 Python 通过 SSH 连接到 Sqlite3 数据库?

java - 构造函数是如何执行的?

java - Android - 即使应用程序关闭时也在后台计算时间

SQL - 查找年份子集中的所有代码

Android View Flipper自定义绘图

android - 在 Android ListView 中重置背景颜色