jjzjj

android - 异常 "table.... has no column named...."

coder 2023-12-25 原文

我在尝试插入名为“my_card”的表时遇到了一个非常奇怪的错误。

这些是我在单独的接口(interface)中定义的一些常量:

//tables name
public static final String TABLE_MY_CARD= "my_card" ;
public static final String TABLE_MY_CONTACTS = "my_contacts" ;

// Columns
public static final String NAME = "name" ;
public static final String PHONE = "phone" ;
public static final String EMAIL = "email";

public static final String FACEBOOK = "facebook";
public static final String WEBSITE = "web";
public static final String MAJOR = "major";
public static final String ULINK = "ulink";

之后我有一个扩展 SQLiteOpenHelper 的类,它有 2 个函数。最重要的是第一个创建参数给定的表。

public class MySQLite_methods extends SQLiteOpenHelper implements Tables {

private static final String DATABASE_NAME = "my_database.db" ;
private static final int DATABASE_VERSION = 1;
private String TABLE_NAME;

/** Create a helper object for the Events database */
public MySQLite_methods (Context ctx, String table_name) 
{
    super(ctx, DATABASE_NAME, null, DATABASE_VERSION);
    this.TABLE_NAME = table_name;
}

@Override
public void onCreate(SQLiteDatabase db) 
{       
    String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + 
                    " (" + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +               
                    NAME + " TEXT NOT NULL," + 
                    PHONE + " TEXT NOT NULL,"+
                    EMAIL + " TEXT NOT NULL,"+                      
                    FACEBOOK + " TEXT,"+
                    WEBSITE + " TEXT,"+
                    MAJOR + " TEXT NOT NULL,"+
                    ULINK + " TEXT NOT NULL" + ");" ;

    db.execSQL(sql);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
{
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    onCreate(db);
}
}

最后,在我的 Activity 中,按下保存按钮后,将创建该类的一个实例,并且必须将一些 EditText-s 的值插入到数据库中:

            MySQLite_methods mysql = new MySQLite_methods (context, TABLE_MY_CARD);


            Log.d("geo", "before getWr....");

            // getting the database..
            SQLiteDatabase db = mysql.getWritableDatabase();

            // creating an object of type ContentValues which represents the record i am gonna add into the DB
            ContentValues values = new ContentValues();

            values.put(NAME, name.getText().toString());
            values.put(PHONE, phone.getText().toString());
            values.put(EMAIL, email.getText().toString());

            values.put(FACEBOOK, facebook.getText().toString());
            values.put(WEBSITE, website.getText().toString());
            values.put(MAJOR, major.getText().toString());
            values.put(ULINK, ulink.getText().toString());

            Log.d("geo", "before insert");

            // inserting data
            db.insertOrThrow(TABLE_MY_CARD, null, values);

在插入时它给出了这个异常:

“android.database.sqlite.SQLiteException:表 my_card 没有名为 web ... 的列。”

我一遍又一遍地检查拼写错误,但没有...所以我现在不知道该怎么办...有什么想法吗? 谢谢

最佳答案

您的创建 SQL 以 CREATE TABLE IF NOT EXISTS 开头。所以我的想法是您在设备已经有该表的情况下添加了 web 列。如果是这种情况,则只需重新安装该应用程序 - 它会删除数据库,因此您可以确定将使用该列创建表。

关于android - 异常 "table.... has no column named....",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8197396/

有关android - 异常 "table.... has no column named...."的更多相关文章

  1. ruby-on-rails - rails : "missing partial" when calling 'render' in RSpec test - 2

    我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou

  2. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  3. ruby - 检查 "command"的输出应该包含 NilClass 的意外崩溃 - 2

    为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar

  4. ruby-on-rails - 迷你测试错误 : "NameError: uninitialized constant" - 2

    我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test

  5. ruby-on-rails - Rails - 乐观锁定总是触发 StaleObjectError 异常 - 2

    我正在学习Rails,并阅读了关于乐观锁的内容。我已将类型为integer的lock_version列添加到我的articles表中。但现在每当我第一次尝试更新记录时,我都会收到StaleObjectError异常。这是我的迁移:classAddLockVersionToArticle当我尝试通过Rails控制台更新文章时:article=Article.first=>#我这样做:article.title="newtitle"article.save我明白了:(0.3ms)begintransaction(0.3ms)UPDATE"articles"SET"title"='dwdwd

  6. ruby-on-rails - 相关表上的范围为 "WHERE ... LIKE" - 2

    我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que

  7. 使用 ACL 调用 upload_file 时出现 Ruby S3 "Access Denied"错误 - 2

    我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file

  8. ruby - #之间? Cooper 的 *Beginning Ruby* 中的错误或异常 - 2

    在Cooper的书BeginningRuby中,第166页有一个我无法重现的示例。classSongincludeComparableattr_accessor:lengthdef(other)@lengthother.lengthenddefinitialize(song_name,length)@song_name=song_name@length=lengthendenda=Song.new('Rockaroundtheclock',143)b=Song.new('BohemianRhapsody',544)c=Song.new('MinuteWaltz',60)a.betwee

  9. ruby - 安装 Ruby 时遇到问题(无法下载资源 "readline--patch") - 2

    当我尝试安装Ruby时遇到此错误。我试过查看this和this但无济于事➜~brewinstallrubyWarning:YouareusingOSX10.12.Wedonotprovidesupportforthispre-releaseversion.Youmayencounterbuildfailuresorotherbreakages.Pleasecreatepull-requestsinsteadoffilingissues.==>Installingdependenciesforruby:readline,libyaml,makedepend==>Installingrub

  10. ruby-on-rails - Prawn PDF : I need to generate nested tables - 2

    我需要一个表,其中行实际上是2行表,一个嵌套表是..我怎样才能在Prawn中做到这一点?也许我需要延期..但哪一个? 最佳答案 现在支持子表:Prawn::Document.generate("subtable.pdf")do|pdf|subtable=pdf.make_table([["sub"],["table"]])pdf.table([[subtable,"original"]])end 关于ruby-on-rails-PrawnPDF:Ineedtogeneratenested

随机推荐