python类定义问题

时间:2015-05-15 06:55:20

标签: python raspberry-pi

这是我的第一个python代码我试图连接到数据库,我在一个名为mysqlConnection.py的python文件中定义了一个名为Database的类

但是当我运行代码时出现此错误:

Traceback (most recent call last):
  File "mysqlConnection.py", line 3, in <module>
    class Database:
  File "mysqlConnection.py", line 27, in Database
    db = Database()

我的代码是:

import MySQLdb

class Database:
        host = "localhost"
        user = "root"
        passwd = "root"
        db = "pitest"

        def __init__(self):
                self.connection = MySQLdb.connect( host = self.host,
                                                   user = self.user,
                                                   passwd = self.passwd,
                                                   db = self.db)
        def query(self, q):
                cursor = self.connection.cursor( MySQLdb.cursors.DictCursor )
                cursor.execute(q)

                return cursor.fetchall()

        def __del__(self):
                self.connection.close()




        if __name__ == '__main__':
                db = Database()

                q = "DELETE FROM users"
                db.query(q)

                q = """
                INSERT INTO users (title, fname, sname, age, email)
                VALUES ('A', 'Z', 'BIG Z', '20', 'raspberrypi@example.com'),
               ('B', 'X', 'BIG X', '30', 'raspberrypi@example.com'),
               ('C', 'Y', 'BIG Y', '24', 'raspberrypi@example.com'),
               ('A', 'W', 'BIG W', '29', 'raspberrypi@example.com')
                """
                db.query(q)
                q = """
                SELECT * FROM users WHERE title = 'A'
                """

                allreturnedusers = db.query(q)

                for user in allreturnedusers:
                        print "Found: %s " % user['fname']

任何人都能帮帮我吗?

1 个答案:

答案 0 :(得分:1)

您的代码从

行开始不正确地缩进
    CLLocationCoordinate2D touchMapCoordinate;

- (void)viewDidLoad {

    [self setUpGesture:YES];
}

- (void)setUpGesture:(BOOL)state
{
    if (state == YES)
    {
        self.longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self
                                                                       action:@selector(handleLongPress:)];
        self.longPress.delegate = self;
        [self.view addGestureRecognizer:self.longPress];

    }
    else
    {
        [self.mapView removeGestureRecognizer:self.longPress];
        self.longPress = nil;
    }

}

- (void)handleLongPress:(UILongPressGestureRecognizer *)recognizer
{
    if (recognizer.state == UIGestureRecognizerStateBegan)
    {
        CGPoint longPressPoint = [recognizer locationInView:self.view];
        [self dropPinAtPoint:longPressPoint];
    }
}


- (void)dropPinAtPoint:(CGPoint)pointToConvert
{
touchMapCoordinate = [self.mapView convertPoint:pointToConvert
                               toCoordinateFromView:self.view];
}

{


                NSMutableDictionary *dict=[[NSMutableDictionary alloc] init];
                [dict setObject:[NSString stringWithFormat:@"%f",touchMapCoordinate.latitude] forKey:@"latitude"];
                [dict setObject:[NSString stringWithFormat:@"%f",touchMapCoordinate.longitude] forKey:@"longitude"];
                [dict setObject:Str_pin forKey:@"title"];

                [ApplicationDelegate.Array_LongpressItem addObject:dict];


                [[NSUserDefaults standardUserDefaults] setObject:ApplicationDelegate.Array_LongpressItem forKey:@"ApplicationDelegate.Array_LongpressItem"];


                    for (id annotation in self.mapView.annotations)
                                        {
                                            [self.mapView removeAnnotation:annotation];

                                        }


                [self loadRoute];

                // add the overlay to the map
                if (nil != self.routeLine) {
                    [self.mapView addOverlay:self.routeLine];
                }

                // zoom in on the route.
                [self zoomInOnRoute];


                for (int i=0; i<[ApplicationDelegate.Array_LongpressItem count]; i++)
                {
//                    NSString *pinTitle = Str_pin;
//                    // NSString *subCoordinates = [NSString stringWithFormat: @"%f, %f", convertedPoint.latitude, convertedPoint.longitude];
//                    
//                    MyAnnotation *point = [[MyAnnotation alloc] init];
//                    point.coordinate = touchMapCoordinate;
//                    point.tag=[NSNumber numberWithInt:2000];
//                    point.title = pinTitle;
//                    
//                    //    for (id annotation in self.mapView.annotations)
//                    //    {
//                    //        [self.mapView removeAnnotation:annotation];
//                    //
//                    //    }
//                    [self.mapView addAnnotation:point];

                    CLLocationDegrees latitude  = [[[ApplicationDelegate.Array_LongpressItem objectAtIndex:i] objectForKey:@"latitude"] floatValue];
                    CLLocationDegrees longitude = [[[ApplicationDelegate.Array_LongpressItem objectAtIndex:i] objectForKey:@"longitude"]floatValue];
                    // create our coordinate and add it to the correct spot in the array
                    CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(latitude, longitude);
                    ann[i] = [[MyAnnotation alloc] init];
                    ann[i].coordinate =coordinate;
                    ann[i].tag=[NSNumber numberWithInt:2000];
                    ann[i].title = [[ApplicationDelegate.Array_LongpressItem objectAtIndex:i] objectForKey:@"title"];
                    [_mapView addAnnotation:ann[i]];

                }
            }

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(MyAnnotation*) annotation
{
if ([annotation.tag intValue]==2000)
    {
        annView.annotation=annotation;
        annView.canShowCallout = YES;
        UIButton *button1 = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        annView.animatesDrop=YES;
        button1.tag=[annotation.tag intValue];
        [button1 addTarget:self action:@selector(btn_clicked:) forControlEvents:UIControlEventTouchUpInside];
        annView.rightCalloutAccessoryView =  button1;
        annView.calloutOffset = CGPointMake(0, 32);

    }
}