如何从数据库sqlite向uipickerview添加值?

发布于 2024-10-17 21:29:34 字数 1154 浏览 2 评论 0原文

我是 sqlite 和编程新手, 我已经尝试了一周,但无法向前推进一点。我已经看到你帮助了很多人,我想你可以解决我的问题。我已经发布了很多,但我没有从任何地方得到任何回复这 。 你可以联系我: [email protected]

首先我必须创建两个在同一视图上单独的 uipickerviews。在该 pickerview 中,我必须从 sqlite 数据库表中插入值。 在第一个视图中,我必须从 sqlite 的第一个表中获取值。在第二个视图中,我必须通过与 id 进行比较来从第二个表中获取值。 视图必须位于同一视图上。

Table1:资格

CREATE TABLE 资格(id INTEGER PRIMARY KEY,名称 VARCHAR(50),描述 TEXT);

INSERT INTO "资格" VALUES(1,'PG','毕业后');

INSERT INTO "资格" VALUES(1,'学位','正在毕业');

INSERT INTO "资格" VALUES(1,'十+二','大学');

表2:课程

CREATE TABLE 课程 (Id Integer PrimaryKey,number Integer,name1 varchar(20),description1 Text(30));

INSERT INTO "课程" VALUES(1,1,'MCA','计算机硕士'); INSERT INTO“课程”VALUES(2,1,'MBA','商业硕士'); INSERT INTO "课程" VALUES(3,2,'BSc','科学学士'); INSERT INTO "课程" VALUES(4,2,'BCom','会计学学士'); INSERT INTO“课程”VALUES(5,3,'MPC','数学'); INSERT INTO“课程”VALUES(5,3,'BPC','生物学');

请参阅第一个表,我将得到值 PG,Degree,Ten+ 两个

当我单击 PG 时,它必须显示在文本字段中,在第二个选择器视图中我必须仅获取与 资格的“id” 当然是“数”。

当我们选择值时,它必须显示在文本字段中 感谢你

Am new to sqlite and also programming,
Am trying this from a week and not able to move a bit forward.I have seen U have helped many and I think I can be solved my problem by you.I have posted a lot,but I didnt get any reply from any where for this .
U can contact me : [email protected]

At first I have to create two separate uipickerviews on same view.In that pickerview I have to insert values from sqlite database tables.
In first view i have to get the values from first table of sqlite.And in second view i have to get the values from second table by comparing with the id.
the views must be on same view.

Table1:qualification

CREATE TABLE qualification ( id INTEGER PRIMARY KEY, name VARCHAR(50), description TEXT);

INSERT INTO "qualification" VALUES(1,'P.G','Post Graduation');

INSERT INTO "qualification" VALUES(1,'Degree','Under Graduation');

INSERT INTO "qualification" VALUES(1,'Ten+Two','college');

Table2:course

CREATE TABLE Course (Id Integer primarykey,number integer,name1 varchar(20),description1 Text(30));

INSERT INTO "Course" VALUES(1,1,'MCA','Master in computers');
INSERT INTO "Course" VALUES(2,1,'MBA','Master in Bussiness');
INSERT INTO "Course" VALUES(3,2,'BSc','Bachelor in sscience');
INSERT INTO "Course" VALUES(4,2,'BCom','Bachelor in accounts');
INSERT INTO "Course" VALUES(5,3,'MPC','Mathematics');
INSERT INTO "Course" VALUES(5,3,'BPC','Biology');

See in first table i will get values P.G,Degree,Ten+
Two

when i click on P.G it must display in textfield and in second pickerview I must get values only which matches with
"id" of qualification with
"number " of course.

And when we select value it must display in textfield
Thank u

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

反话 2024-10-24 21:29:34

我做了一个项目来向您展示如何做您所要求的事情。

您可以从这里下载该项目:

http://www.crowsoft.com.ar/downloads /twoPickerViews.zip

可以在这里找到 iPhone 中 sqlite 的非常好的教程:

http://www.icodeblog.com/2008/08/19/iphone-programming-tutorial-creating-a-todo-list -using-sqlite-part-1/

要管理 PickerView,您需要一个数组来保存数据。

我声明了两个类(资格和课程)来表示您的表和两个 NSMutableArray 变量来保存数据。

要链接两个 PickerView,您需要在视图控制器中实现 UIPickerViewDataSource 和 UIPickerViewDelegate:

@interface twoPickerViewsViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate> {

要在用户选择资格时做出响应,您需要编写 didSelectRow:

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
   // And now the final part of the UIPickerView, what happens when a row is selected.
   if (thePickerView == qualificationsPicker) {
       if (row >= 0 && row < [m_qualifications count]) {
           m_selectedQualification = [m_qualifications objectAtIndex:row];    
           qualificationName.text = m_selectedQualification.qualificationName;
           [self getCourses];
           [coursesPicker reloadAllComponents];
       }
   }
}

您需要编写另外三个方法,numberOfComponentsInPickerView、numberOfRowsInComponent、titleForRow:

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
   // This method needs to be used. It asks how many columns will be used in the UIPickerView
   return 1; // We only need one column so we will return 1.
}

- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
   // This method also needs to be used. This asks how many rows the UIPickerView will have.
   if (thePickerView == qualificationsPicker) {
       return [m_qualifications count]; // We will need the amount of rows that we used in the pickerViewArray, so we will return the count of the array.
   }
   else {
       return [self getCourses];
   }
}

- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
   // This method asks for what the title or label of each row will be.
   if (thePickerView == qualificationsPicker) {
       Qualification *qualification = [m_qualifications objectAtIndex:row];
       return qualification.qualificationName; // We will set a new row for every string used in the array.
   }
   else {
       Course *course = [m_selectedCourses objectAtIndex:row];
       return course.courseName;
   }    
}

I have made a project to show you how to do what you are asking for.

You can download the project from here:

http://www.crowsoft.com.ar/downloads/twoPickerViews.zip

A very good tutorial for sqlite in iPhone could be found here:

http://www.icodeblog.com/2008/08/19/iphone-programming-tutorial-creating-a-todo-list-using-sqlite-part-1/

To manage a PickerView you need an array to hold the data.

I have declared two classes (Qualification and Course) to represent your tables and two NSMutableArray variables to hold the data.

To link the two PickerViews you need to implement UIPickerViewDataSource and UIPickerViewDelegate in your view controler:

@interface twoPickerViewsViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate> {

to respond when the user select a qualification you need to code didSelectRow:

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
   // And now the final part of the UIPickerView, what happens when a row is selected.
   if (thePickerView == qualificationsPicker) {
       if (row >= 0 && row < [m_qualifications count]) {
           m_selectedQualification = [m_qualifications objectAtIndex:row];    
           qualificationName.text = m_selectedQualification.qualificationName;
           [self getCourses];
           [coursesPicker reloadAllComponents];
       }
   }
}

You need to code three more methods, numberOfComponentsInPickerView, numberOfRowsInComponent, titleForRow:

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
   // This method needs to be used. It asks how many columns will be used in the UIPickerView
   return 1; // We only need one column so we will return 1.
}

- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
   // This method also needs to be used. This asks how many rows the UIPickerView will have.
   if (thePickerView == qualificationsPicker) {
       return [m_qualifications count]; // We will need the amount of rows that we used in the pickerViewArray, so we will return the count of the array.
   }
   else {
       return [self getCourses];
   }
}

- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
   // This method asks for what the title or label of each row will be.
   if (thePickerView == qualificationsPicker) {
       Qualification *qualification = [m_qualifications objectAtIndex:row];
       return qualification.qualificationName; // We will set a new row for every string used in the array.
   }
   else {
       Course *course = [m_selectedCourses objectAtIndex:row];
       return course.courseName;
   }    
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文