【问题标题】:Database with 5 Tables with Insert and Select带有插入和选择的 5 个表的数据库
【发布时间】:2011-01-06 10:05:22
【问题描述】:

我的问题是我有 5 个表,需要插入和选择。

我所做的是为每个表创建一个类,并在那里编写这样的 SQL 语句

    public class Contact

private static String IDCont = "id_contact";
    private static String NameCont = "name_contact";
    private static String StreetCont = "street_contact";
    private static String Street2Cont = "street2_contact";
    private static String Street3Cont = "street3_contact";
    private static String ZipCont = "zip_contact";
    private static String CityCont = "city_contact";
    private static String CountryCont = "country_contact";
    private static String Iso2Cont = "iso2_contact";
    private static String PhoneCont = "phone_contact";
    private static String Phone2Cont = "phone2_contact";
    private static String FaxCont = "fax_contact";
    private static String MailCont = "mail_contact";
    private static String Mail2Cont = "mail2_contact";
    private static String InternetCont = "internet_contact";
    private static String DrivemapCont = "drivemap_contact";
    private static String PictureCont = "picture_contact";
    private static String LatitudeCont = "latitude_contact";
    private static String LongitudeCont = "longitude_contact";
    public static final String TABLE_NAME = "contact";
        public static final String SQL_CREATE = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + "(" +
        IDCont + "INTEGER not NULL," +
        NameCont + " TEXT not NULL," +
        StreetCont + " TEXT," +
        Street2Cont + " TEXT," +
        Street3Cont + " TEXT," +
        ZipCont + " TEXT," +
        CityCont + " TEXT," +
        CountryCont + " TEXT," +
        Iso2Cont + " TEXT," +
        PhoneCont + " TEXT," +
        Phone2Cont + " TEXT," +
        FaxCont + " TEXT," +                
        MailCont + " TEXT," +
        Mail2Cont + " TEXT," +
        InternetCont + " TEXT," +       //website of the contact
        DrivemapCont + " TEXT," +       //a link to a drivemap to the contact
        PictureCont + " TEXT," +            //a photo of the contact building (contact is not a person)
        LatitudeCont + " TEXT," +
        LongitudeCont + " TEXT," +
        "primary key(id_contact)" +
        "foreign key(iso2)";

and my insert looks like this

    public boolean SQL_INSERT_CONTACT(int IDContIns, String NameContIns, String StreetContIns,
                    String Street2ContIns, String Street3ContIns, String ZipContIns, 
                    String CityContIns, String CountryContIns, String Iso2ContIns,
                    String PhoneContIns, String Phone2ContIns, String FaxContIns,
                    String MailContIns, String Mail2ContIns, String InternetContIns,
                    String DrivemapContIns, String PictureContIns, String LatitudeContIns,
                    String LongitudeContIns) {
        try{
        db.execSQL("INSERT INTO " + "contact" +
                "(" + IDCont + ", " + NameCont + ", " + StreetCont + ", " + 
                Street2Cont + ", " + Street3Cont + ", " + ZipCont + ", " + 
                CityCont + ", " + CountryCont + ", " + Iso2Cont + ", " + 
                PhoneCont + ", " + Phone2Cont + ", " + FaxCont + ", " + 
                MailCont + ", " + Mail2Cont + ", " + InternetCont + ", " + 
                DrivemapCont + ", " + PictureCont + ", " + LatitudeCont + ", " + 
                LongitudeCont + ") " +
                "VALUES (" + IDContIns + ", " + NameContIns +", " + StreetContIns + ", " +
                Street2ContIns + ", " + Street3ContIns + ", " + ZipContIns + ", " +
                CityContIns + ", " + CountryContIns + ", " + Iso2ContIns + ", " + 
                PhoneContIns + ", " + Phone2ContIns + ", " + FaxContIns + ", " + 
                MailContIns + ", " + Mail2ContIns + ", " + InternetContIns + ", " + 
                DrivemapContIns + ", " + PictureContIns + ", " + LatitudeContIns + ", " + 
                LongitudeContIns +")");
        return true;
        }
        catch (SQLException e) {
            return false;   
        }

    }

我有一个 DBAdapter 类在那里我创建了数据库

 public class DBAdapter {

         public static final String DB_NAME = "mol.db";
            private static final int DB_VERSION = 1;
            private static final String TAG = "DBAdapter";      //to log

            private final Context context;
            private SQLiteDatabase db;

        public DBAdapter(Context context) 
            {
                this.context = context;
                OpenHelper openHelper = new OpenHelper(this.context);
                this.db = openHelper.getWritableDatabase();
        }

     public static class OpenHelper extends SQLiteOpenHelper 
        {

            public OpenHelper(Context context) {
            super(context, DB_NAME, null, DB_VERSION);
        }
    @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
            db.execSQL(Contact.SQL_CREATE);
            db.execSQL(Country.SQL_CREATE);
            db.execSQL(Picture.SQL_CREATE);
            db.execSQL(Product.SQL_CREATE);
            db.execSQL(Project.SQL_CREATE);

        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub
            Log.w(TAG, "Upgrading database from version "
                    + oldVersion + " to " + newVersion
                    + ", which will destroy all old data");
            db.execSQL(Contact.SQL_DROP);
            db.execSQL(Country.SQL_DROP);
            db.execSQL(Picture.SQL_DROP);
            db.execSQL(Product.SQL_DROP);
            db.execSQL(Project.SQL_DROP);
            onCreate(db);
        }

我发现了很多不同的东西并尝试了它们,但我没有得到任何工作...... 我需要知道如何在我的活动中访问数据库 以及如何让插入工作 我的代码有问题吗? 感谢您的帮助

这就是我尝试将其纳入我的活动的方式

public class MainTabActivity extends TabActivity {


private Context context;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.maintabactivity);

    TabHost mTabHost = getTabHost();



    Intent intent1 = new Intent().setClass(this,MapOfLight.class);
    //Intent intent2 = new Intent().setClass(this,Test.class);              //Testactivity
    //Intent intent2 = new Intent().setClass(this,DetailView.class);        //DetailView
    Intent intent2 = new Intent().setClass(this,ObjectList.class);      //ObjectList
    //Intent intent2 = new Intent().setClass(this,Gallery.class);           //Gallery
    Intent intent3 = new Intent().setClass(this,ContactDetail.class);
    mTabHost.addTab(mTabHost.newTabSpec("tab_mol").setIndicator(this.getText(R.string.mol), getResources().getDrawable(R.drawable.ic_tab_mol)).setContent(intent1));
    mTabHost.addTab(mTabHost.newTabSpec("tab_highlights").setIndicator(this.getText(R.string.highlights),getResources().getDrawable(R.drawable.ic_tab_highlights)).setContent(intent2));
    mTabHost.addTab(mTabHost.newTabSpec("tab_contacts").setIndicator(this.getText(R.string.contact),getResources().getDrawable(R.drawable.ic_tab_contact)).setContent(intent3));

    mTabHost.setCurrentTab(1);



    SQLiteDatabase db;
    DBAdapter dh = null;
    OpenHelper openHelper = new OpenHelper(this.context);


    dh = new DBAdapter(this);
    db = openHelper.getWritableDatabase();

    dh.SQL_INSERT_COUNTRY("AT", "Austria", "AUT");

}

}

我在我的国家/地区表中尝试过,因为它只有 3 列

public class Country {

    private static String Iso2Count = "iso2_country";
    private static String NameCount = "name_country";
    private static String FlagCount = "flag_image_url_country";

    public static final String TABLE_NAME = "country";
    public static final String SQL_CREATE = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + "(" +
    Iso2Count + " TEXT not NULL," +
    NameCount + " TEXT not NULL," +
    FlagCount + " TEXT not NULL," +
    "primary key(iso2_country)";

    public boolean SQL_INSERT_COUNTRY(String Iso2CountIns, String NameCountIns, String FlagCountIns) {
        try{
        db.execSQL("INSERT INTO " + "country" +
                "(" + Iso2Count + ", " + NameCount + ", " + FlagCount + ") " +
                "VALUES ( " + Iso2CountIns + ", " + NameCountIns +", " + FlagCountIns + " )");
        return true;
        }
        catch (SQLException e) {
            return false;   
        }

    }

另一个问题是将每个表中的插入和选择放入一个单独的类中更好,所以我为每个表有一个类还是将它们全部放入 DBAdapter 类中?

【问题讨论】:

  • 什么不起作用?你有任何错误吗?有什么吗?
  • 请具体说明您的问题。无法连接到数据库?插入失败? ...不要忘记显示相关的错误消息。
  • 我没有收到任何错误消息,它只是强制关闭。
  • 我建议您提取您尝试执行的 SQL 语句,并使用适合您服务器的工具直接在数据库上运行查询(您没有说您使用的是哪个:SQL Server、Oracle等)一旦你知道这些语句有效,你就可以将它们移回你的代码中。
  • 我正在编程android,我正在使用SQLite

标签: sql android sqlite select insert


【解决方案1】:

编辑:我不确定您是否已显示所有代码,因此我不确定我的回答是否有帮助。您在哪里声明变量 IDCont、NameCont、StreetCont ... ?我假设它们是定义字段名称的常量。在这种情况下,我的回答不正确,因为这应该构成一个有效的INSERT 声明。

您能否在您使用的数据库服务器上添加您看到的错误消息以及更具体的信息?


您的 INSERT 语句将不起作用,因为您插入的是传递给函数的变量的值,而不是指定列的名称。

语句的第一部分应该是一个字符串:

"INSERT INTO contact (IDCont, NameCont, StreetCont, Street2Cont, Street3Cont, ZipCont," + 
    "CityCont, CountryCont, Iso2Cont, PhoneCont, Phone2Cont, FaxCont, MailCont, " +
    "Mail2Cont, InternetCont, DrivemapCont, PictureCont, LatitudeCont, LongitudeCont) " 
"VALUES ("IDContIns + ", " + NameContIns +", " + StreetContIns + ", " +
            Street2ContIns + ", " + Street3ContIns + ", " + ZipContIns + ", " +
            CityContIns + ", " + CountryContIns + ", " + Iso2ContIns + ", " + 
            PhoneContIns + ", " + Phone2ContIns + ", " + FaxContIns + ", " + 
            MailContIns + ", " + Mail2ContIns + ", " + InternetContIns + ", " + 
            DrivemapContIns + ", " + PictureContIns + ", " + LatitudeContIns + ", " + 
            LongitudeContIns +")"

我还要质疑你的表格设计,为什么大部分字段都是TEXT 而不是更合适的数据类型?

【讨论】:

  • 是的,我忘了添加它们,这些是确切的列名。我需要知道如何在我的活动中访问数据库以及如何调用插入。我添加了我上次尝试的方式。
  • “更合适的类型”是什么意思?它们都是文本。
  • 是的,它们包含文本,但它们都需要能够存储大量文本吗? MySQL 文档说“对于 BLOB 和 TEXT 数据,信息在内部存储在与行缓冲区不同的内存区域中”,这可能导致性能问题“使用处理的查询结果中的 BLOB 或 TEXT 列实例临时表会导致服务器使用磁盘上的表而不是内存中的表......使用磁盘会导致性能下降,因此只有在真正需要时才在查询结果中包含 BLOB 或 TEXT 列。”
  • 你的意思是像 nvarchar(10)?我记得,我读过有关 android/SQLite 的信息,您不必指定文本的长度,因为它会自动提供所需的空间(动态)。
  • 抱歉,我假设您使用的是基于服务器的 SQL 引擎,因为您没有在问题或标签中指定它们。在这种情况下,我不是 SQLite 或 Android 方面的专家,因此其他人可能会提供帮助。
猜你喜欢
  • 2012-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-25
  • 2023-03-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多